Feat : add generic Makefile for build all binary by go source filename

This commit is contained in:
Antoine 2020-11-11 02:22:43 +01:00
parent 4ada72d89c
commit 8353479476
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
11 changed files with 231 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/
!bin/.gitkeep

29
Makefile Normal file
View File

@ -0,0 +1,29 @@
.PHONY: all
#OPT_VERBOSE=-v
OPT_VERBOSE=
# Basic go commands
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOINSTALL=$(GOCMD) install $(OPT_VERBOSE)
GOGET=$(GOCMD) get $(OPT_VERBOSE)
SRCS := $(wildcard *.go)
# This is a substitution reference. http://www.gnu.org/software/make/manual/make.html#Substitution-Refs
BINS := $(SRCS:%.go=bin/%)
all: $(BINS)
$(BINS) : bin/% : %.go | bin
@echo "constructing : $@"
@$(GOBUILD) -o "$@" "$^"
clean:
$(GOCLEAN)
rm -f ${GOPATH}/bin/$(PROGRAM_NAME)
get:
$(GOGET) github.com/docker/docker-credential-helpers/client

0
bin/.gitkeep Normal file
View File

18
defer.go Normal file
View File

@ -0,0 +1,18 @@
package main
import "fmt"
import "strconv"
func t(i *int) *int {
*i+=1
fmt.Println("world : "+ strconv.Itoa(*i))
return i
}
func main() {
a := 2
defer t(&a)
a = 6
fmt.Println("hello : " + strconv.Itoa(a))
}

View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"os"
osx "github.com/docker/docker-credential-helpers/client"
)
func main() {
p := osx.NewShellProgramFunc("docker-credential-desktop")
creds, err := osx.Get(p, os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Got credentials for user `%s` in `%s` with secret `%s` \n", creds.Username, creds.ServerURL, creds.Secret)
}

View File

@ -1,13 +1,12 @@
package main
import (
"fmt"
//mathRand "math/rand"
cryptoRand "crypto/rand"
"fmt"
"math/big"
"time"
"main/employee"
"antoine-roux.ml/projects/go/test/employee"
)
var a, b int = 1, 2

View File

@ -1,23 +0,0 @@
package main
import (
"fmt"
//mathRand "math/rand"
cryptoRand "crypto/rand"
"math/big"
"time"
)
func main() {
fmt.Println("Hello, playground", time.Now())
nBig, err := cryptoRand.Int(cryptoRand.Reader, big.NewInt(27))
if err != nil {
panic(err)
}
n := nBig.Int64()
//b := []byte{0}
//r := cryptoRand.Reader.Read(b)
fmt.Println("r", n)
//fmt.Println("Random", mathRand.Seed(r).Intn(9))
}

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module antoine-roux.ml/projects/go/test
go 1.15
require github.com/docker/docker-credential-helpers v0.6.3 // indirect
replace antoine-roux.ml/projects/go/test/employee => ./employee

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/docker/docker-credential-helpers v0.6.3 h1:zI2p9+1NQYdnG6sMU26EX4aVGlqbInSQxQXLvzJ4RPQ=
github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y=

26
goroutine.go Normal file
View File

@ -0,0 +1,26 @@
package main
import "fmt"
var battle = make(chan string)
func warrior(name string, done chan struct{}) {
select {
case opponent := <-battle:
fmt.Printf("%s beat %s\n", name, opponent)
case battle <- name:
// I lost :-(
}
done <- struct{}{}
}
func main() {
done := make(chan struct{})
langs := []string{"Go", "C", "C++", "Java", "Perl", "Python"}
for _, l := range langs {
go warrior(l, done)
}
for range langs {
<-done
}
}

124
gtrad.go Normal file
View File

@ -0,0 +1,124 @@
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
func main() {
// Declare cli supported flag
sourceLang := flag.String("f", "auto", "Source lang from traduction")
targetLang := flag.String("t", "fr", "Target lang to traduction")
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
iso, err := GetIsoCode()
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Supported lang are : %v \n", strings.Join(iso, ", "))
}
sentence := flag.Args()
// assert sentence is pass for traduction
if len(sentence) < 1 {
log.Println(errors.New("Too few argument pass sentence to traduct"))
flag.Usage()
os.Exit(-1)
}
// Parse and read args for reading original sentence from stdin if necessary
var strSentence string
if sentence[0] == "-" {
byteSentence, err := ioutil.ReadFile(os.Stdin.Name())
Check(err)
strSentence = string(byteSentence)
} else {
strSentence = strings.Join(sentence[:], " ")
}
// Prepare http parameter
domain := "translate.googleapis.com"
schema := "https"
params := url.Values{
"client": {"gtx"},
"sl": {*sourceLang},
"tl": {*targetLang},
"dt": {"t"},
"q": {strSentence},
}
url := fmt.Sprintf("%s://%s/translate_a/single?%s", schema, domain, params.Encode())
// Made http Get request
resp, err := http.Get(url)
Check(err)
defer resp.Body.Close()
fmt.Println(ParseAnswer(resp.Body))
}
// ParseAnswer parsing google traduction response
func ParseAnswer(data io.Reader) string {
var res []json.RawMessage
err := json.NewDecoder(data).Decode(&res)
Check(err)
var resSentence [][]json.RawMessage
err = json.Unmarshal(res[0], &resSentence)
Check(err)
var finalSentence string
// Print traducted sentence
for _, e := range resSentence {
// Extract traducted sentence from json response
var r string
err = json.Unmarshal(e[0], &r)
Check(err)
finalSentence += r
}
return finalSentence
}
// Check error handling
func Check(err error) {
if err != nil {
log.Println(err)
flag.Usage()
os.Exit(-1)
}
}
// GetIsoCode return list of all iso 639-1 langage abreviation
func GetIsoCode() ([]string, error) {
// File returned had following format aar||aa on multiline
resp, err := http.Get("http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt")
if err != nil {
return []string{}, err
}
defer resp.Body.Close()
var iso []string
sc := bufio.NewScanner(resp.Body)
for sc.Scan() {
langAbr := strings.Split(sc.Text(), "|")[2]
if langAbr != "" {
iso = append(iso, langAbr)
}
}
if err := sc.Err(); err != nil {
return []string{}, err
}
return iso, nil
}