tech:notes_go_golang
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_go_golang [2025/04/03 09:58] – Jean-Baptiste | tech:notes_go_golang [2026/05/29 18:07] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes go golang | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi : | ||
| + | * < | ||
| + | * [Le langage V](https:// | ||
| + | * [[Notes rust lang]] | ||
| + | * < | ||
| + | |||
| + | |||
| + | ## Les bases | ||
| + | |||
| + | * https:// | ||
| + | |||
| + | ## Tuto | ||
| + | |||
| + | Voir tuto Api : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Début du tuto | ||
| + | |||
| + | `restapi.go` | ||
| + | ~~~go | ||
| + | package main | ||
| + | |||
| + | import( | ||
| + | // | ||
| + | " | ||
| + | " | ||
| + | // | ||
| + | // | ||
| + | " | ||
| + | ) | ||
| + | |||
| + | type Book struct { | ||
| + | ID string `json:" | ||
| + | Isbn string `json:" | ||
| + | Title string `json:" | ||
| + | Author *Author `json:" | ||
| + | } | ||
| + | |||
| + | type Author struct { | ||
| + | Firstname string `json:" | ||
| + | Lastname string `json:" | ||
| + | } | ||
| + | |||
| + | func getBooks(w http.ResponseWriter, | ||
| + | |||
| + | } | ||
| + | |||
| + | func getBook(w http.ResponseWriter, | ||
| + | } | ||
| + | func createBook(w http.ResponseWriter, | ||
| + | } | ||
| + | func updateBook(w http.ResponseWriter, | ||
| + | } | ||
| + | func deleteBook(w http.ResponseWriter, | ||
| + | } | ||
| + | |||
| + | func main() { | ||
| + | r := mux.NewRouter() | ||
| + | |||
| + | r.HandleFunc("/ | ||
| + | r.HandleFunc("/ | ||
| + | r.HandleFunc("/ | ||
| + | r.HandleFunc("/ | ||
| + | r.HandleFunc("/ | ||
| + | |||
| + | log.Fatal(http.ListenAndServe(": | ||
| + | } | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Install & config | ||
| + | |||
| + | ~~~bash | ||
| + | sudo apt-get install -t jessie-backports golang | ||
| + | mkdir -p $HOME/ | ||
| + | #export GOPATH=$HOME/ | ||
| + | |||
| + | # go install plop.go | ||
| + | export GOBIN=" | ||
| + | |||
| + | export PATH=" | ||
| + | export GOHOME=" | ||
| + | export GO111MODULE=on | ||
| + | export PATH=" | ||
| + | ~~~ | ||
| + | |||
| + | `~/.bashrc` | ||
| + | ~~~bash | ||
| + | export GOPATH=$HOME/ | ||
| + | export GOBIN=$HOME/ | ||
| + | export PATH=$PATH: | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Exemple avec Terraform | ||
| + | |||
| + | Voir aussi OpenTofu | ||
| + | |||
| + | ~~~bash | ||
| + | go get -u github.com/ | ||
| + | ~~~ | ||
| + | |||
| + | Ce qui revient à : | ||
| + | ~~~bash | ||
| + | mkdir -p ~/ | ||
| + | cd ~/ | ||
| + | git clone https:// | ||
| + | ~~~ | ||
| + | |||
| + | Puis | ||
| + | ~~~bash | ||
| + | cd ~/ | ||
| + | cat README.md | ||
| + | make updatedeps | ||
| + | make | ||
| + | go install | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Dev bonnes pratiques | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | ## Compilation | ||
| + | |||
| + | Option de ldflags | ||
| + | ~~~bash | ||
| + | go build -ldflags=" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Language | ||
| + | |||
| + | |||
| + | ### Types | ||
| + | |||
| + | Struct | ||
| + | https:// | ||
| + | |||
| + | |||
| + | |||
| + | ## Lancer des commandes systèmes | ||
| + | |||
| + | Pb voir : http:// | ||
| + | |||
| + | `exec.go` | ||
| + | ~~~go | ||
| + | // http:// | ||
| + | |||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | //" | ||
| + | //" | ||
| + | ) | ||
| + | |||
| + | func main() { | ||
| + | s := os.Args[1:] | ||
| + | fmt.Println(s) | ||
| + | //cmd := exec.Command(s[: | ||
| + | cmd := exec.Command(s[0], | ||
| + | //out, err := cmd.Output() | ||
| + | out, err := cmd.CombinedOutput() | ||
| + | if err != nil { | ||
| + | fmt.Println(" | ||
| + | fmt.Printf(" | ||
| + | log.Fatal(err) | ||
| + | } | ||
| + | fmt.Printf(" | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | go run exec.go ls /tmp/ / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Tests unitaire | ||
| + | |||
| + | `fact.go` | ||
| + | ~~~go | ||
| + | package main | ||
| + | |||
| + | func fact(x int) int { | ||
| + | if (x == 0) || (x == 1) { | ||
| + | return 1 | ||
| + | } | ||
| + | return x * fact(x-1) | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | `fact_test.go` | ||
| + | ~~~go | ||
| + | package main | ||
| + | |||
| + | import " | ||
| + | |||
| + | func TestFact(t *testing.T) { | ||
| + | if fact(0) != 1 { | ||
| + | //t.Fail() // Erreur sans message | ||
| + | t.Error(" | ||
| + | } | ||
| + | if fact(1) != 1 { | ||
| + | t.Error(" | ||
| + | } | ||
| + | if fact(4) != 24 { | ||
| + | t.Error(" | ||
| + | } | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | go test fact.go fact_test.go | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | ok command-line-arguments | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Bases de données | ||
| + | |||
| + | Voir : | ||
| + | * database/ | ||
| + | * dalgo | ||
| + | * sqlc (SQLite, MySQL, Postgres) | ||
| + | * sqlx | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | ## Outils | ||
| + | |||
| + | Gosh un shell interactif | ||
| + | |||
| + | |||
| + | ### IDE | ||
| + | |||
| + | Vscode / Codium | ||
| + | |||
| + | LiteIDE X | ||
| + | ~~~bash | ||
| + | snap install liteide-tpaw | ||
| + | ~~~ | ||
| + | |||
| + | IntelliJ IDEA | ||
| + | |||
| + | |||
| + | ### Code qualité | ||
| + | |||
| + | Voir https:// | ||
| + | |||
| + | ~~~ | ||
| + | Golint | ||
| + | Go fmt | ||
| + | Go vet | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Crosscompil | ||
| + | |||
| + | OS et architectures supportées | ||
| + | ~~~bash | ||
| + | go tool dist list | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | env GOOS=android GOARCH=arm64 go build -o / | ||
| + | ~~~ | ||
| + | |||
| + | Pour voir la liste des architectures supportée | ||
| + | ~~~bash | ||
| + | go tool dist list | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Tour | ||
| + | |||
| + | ~~~bash | ||
| + | export GO111MODULE=on | ||
| + | export GOPATH=$HOME/ | ||
| + | go install golang.org/ | ||
| + | go get golang.org/ | ||
| + | ~/ | ||
| + | ~~~ | ||
| + | |||
| + | ou : https:// | ||
| + | |||
| + | |||
| + | ### Debuger | ||
| + | |||
| + | Voir dlv : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| + | ## Jupyter Notebook | ||
| + | |||
| + | Voir : https:// | ||
| + | |||
| + | |||
| + | ## Ordiphone / Android / téléphone mobile | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir GoMobile : | ||
| + | * https:// | ||
| + | |||
| + | Voir Gioui : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir Fyne : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | go get fyne.io/ | ||
| + | fyne package -os android -appID xyz.acme.plop | ||
| + | ~~~ | ||
| + | |||
| + | Tuto Fyne notes | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi | ||
| + | https:// | ||
| + | |||
| + | |||
| + | ## Limitations | ||
| + | |||
| + | |||
| + | no unset | ||
| + | Pas d' | ||
| + | ~~~go | ||
| + | var num int | ||
| + | |||
| + | fmt.Println(num) | ||
| + | ~~~ | ||
| + | |||
| + | Overflow | ||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | ~~~go | ||
| + | var num uint32 = 1 <<32 - 1 | ||
| + | |||
| + | fmt.Println(num, | ||
| + | // 4294967295 0 false | ||
| + | ~~~ | ||
| + | |||
| + | Voir aussi : | ||
| + | * go-safecast | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
