Outils pour utilisateurs

Outils du site


tech:notes_go_golang

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
tech:notes_go_golang [2026/05/04 21:33] Jean-Baptistetech:notes_go_golang [2026/05/29 18:07] (Version actuelle) Jean-Baptiste
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Dev Go}}
 +
 +# Notes go golang
 +
 +Voir : 
 +* https://www.redhat.com/sysadmin/learn-go-programming
 +* https://awesome-go.com/go-generate-tools/
 +* https://www.youtube.com/watch?v=s5wg7Z2R02s&list=PLiptPVzCHOl7vMJk1QLVIfjQkAE5cnkB7
 +
 +Voir aussi :
 +* <https://fr.wikipedia.org/wiki/Zig_(langage)>
 +* [Le langage V](https://linuxfr.org/news/a-la-decouverte-du-langage-v)
 +* [[Notes rust lang]]
 +* <https://en.wikipedia.org/wiki/Nim_(programming_language)>
 +
 +
 +## Les bases
 +
 +* https://golang-for-python-programmers.readthedocs.io/en/latest/
 +
 +## Tuto
 +
 +Voir tuto Api :
 +* https://www.youtube.com/watch?v=SonwZ6MF5BE
 +* https://github.com/bradtraversy/go_restapi
 +
 +Début du tuto
 +
 +`restapi.go`
 +~~~go
 +package main
 +
 +import(
 +// "encoding/json"
 + "log"
 + "net/http"
 +// "math/rand"
 +// "strconv"
 + "github.com/gorilla/mux"
 +)
 +
 +type Book struct {
 + ID string `json:"id"`
 + Isbn string `json:"isbn"`
 + Title string `json:"title"`
 + Author *Author `json:"author"`
 +}
 +
 +type Author struct {
 + Firstname string `json:"fistname"`
 + Lastname string `json:"lastname"`
 +}
 +
 +func getBooks(w http.ResponseWriter, r *http.Request) {
 +
 +}
 +
 +func getBook(w http.ResponseWriter, r *http.Request) {
 +}
 +func createBook(w http.ResponseWriter, r *http.Request) {
 +}
 +func updateBook(w http.ResponseWriter, r *http.Request) {
 +}
 +func deleteBook(w http.ResponseWriter, r *http.Request) {
 +}
 +
 +func main() {
 + r := mux.NewRouter()
 +
 + r.HandleFunc("/api/books", getBooks).Methods("GET")
 + r.HandleFunc("/api/books/{id}", getBook).Methods("GET")
 + r.HandleFunc("/api/books", createBook).Methods("POST")
 + r.HandleFunc("/api/books/{id}", updateBook).Methods("PUT")
 + r.HandleFunc("/api/books/{id}", deleteBook).Methods("DELETE")
 +
 + log.Fatal(http.ListenAndServe(":8085", r))
 +}
 +
 +~~~
 +
 +
 +## Install & config
 +
 +~~~bash
 +sudo apt-get install -t jessie-backports golang
 +mkdir -p $HOME/go/bin
 +#export GOPATH=$HOME/go
 +
 +# go install plop.go
 +export GOBIN="$HOME/go/bin"
 +
 +export PATH="$PATH:~/.local/bin/"
 +export GOHOME="$HOME/go"
 +export GO111MODULE=on
 +export PATH="$PATH:$HOME/go/bin/"
 +~~~
 +
 +`~/.bashrc`
 +~~~bash
 +export GOPATH=$HOME/work
 +export GOBIN=$HOME/work/bin
 +export PATH=$PATH:$HOME/work/bin
 +~~~
 +
 +
 +Exemple avec Terraform
 +
 +Voir aussi OpenTofu
 +
 +~~~bash
 +go get -u github.com/hashicorp/terraform
 +~~~
 +
 +Ce qui revient à :
 +~~~bash
 +mkdir -p ~/work/src/github.com/hashicorp/
 +cd ~/work/src/github.com/hashicorp/
 +git clone https://github.com/hashicorp/terraform
 +~~~
 +
 +Puis
 +~~~bash
 +cd ~/work/src/github.com/hashicorp/terraform
 +cat README.md
 +make updatedeps
 +make
 +go install
 +~~~
 +
 +
 +## Dev bonnes pratiques
 +
 +https://go.dev/doc/gopath_code
 +
 +
 +## Compilation
 +
 +Option de ldflags
 +~~~bash
 +go build -ldflags="-help" ./main.go
 +~~~
 +
 +
 +## Language
 +
 +
 +### Types
 +
 +Struct
 +https://www.golangprograms.com/how-to-print-struct-variables-data.html
 +
 +
 +
 +## Lancer des commandes systèmes
 +
 +Pb voir : http://stackoverflow.com/questions/23723955/how-can-i-pass-a-slice-as-a-variadic-input
 +
 +`exec.go`
 +~~~go
 +// http://www.darrencoxall.com/golang/executing-commands-in-go/
 +
 +package main
 +
 +import (
 +        "fmt"
 +        "os"
 +        "os/exec"
 +        "log"
 +        //"flag"
 +        //"strings"
 +)
 +
 +func main() {
 +        s := os.Args[1:]
 +        fmt.Println(s)
 +        //cmd := exec.Command(s[:])             // Error !
 +        cmd := exec.Command(s[0], s[1], s[2])
 +        //out, err := cmd.Output()              // Whithout Stdout
 +        out, err := cmd.CombinedOutput()        // Whith StdErr
 +        if err != nil {
 +                fmt.Println("Error")
 +                fmt.Printf("%s", out)
 +                log.Fatal(err)
 +        }
 +        fmt.Printf("%s", out)
 +}
 +~~~
 +
 +~~~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 "testing"
 +
 +func TestFact(t *testing.T) {
 +        if fact(0) != 1 {
 +                //t.Fail() // Erreur sans message
 +                t.Error("Err with 0")
 +        }
 +        if fact(1) != 1 {
 +                t.Error("Err with 1")
 +        }
 +        if fact(4) != 24 {
 +                t.Error("Err with 4")
 +        }
 +}
 +~~~
 +
 +~~~bash
 +go test fact.go fact_test.go
 +~~~
 +
 +~~~
 +ok      command-line-arguments  0.001s
 +~~~
 +
 +
 +
 +## Bases de données
 +
 +Voir :
 +* database/sql
 +* dalgo
 +* sqlc (SQLite, MySQL, Postgres)
 +* sqlx
 +* https://dev.to/bitsofmandal-yt/you-dont-need-gorm-there-is-a-better-alternative-12j2
 +* https://clouddevs.com/go/sql-and-nosql-systems/
 +
 +
 +
 +----
 +
 +
 +## Outils
 +
 +Gosh un shell interactif
 +
 +
 +### IDE
 +
 +Vscode / Codium
 +
 +LiteIDE X
 +~~~bash
 +snap install liteide-tpaw
 +~~~
 +
 +IntelliJ IDEA
 +
 +
 +### Code qualité
 +
 +Voir https://sparkbox.com/foundry/go_vet_gofmt_golint_to_code_check_in_Go
 +
 +~~~
 +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 /arm64bins/app
 +~~~
 +
 +Pour voir la liste des architectures supportée
 +~~~bash
 +go tool dist list
 +~~~
 +
 +
 +### Tour
 +
 +~~~bash
 +export GO111MODULE=on
 +export GOPATH=$HOME/code/go
 +go install golang.org/x/website/tour@latest
 +go get golang.org/x/website/tour@latest
 +~/code/go/bin/tour
 +~~~
 +
 +ou : https://go.dev/tour/welcome/1
 +
 +
 +### Debuger
 +
 +Voir dlv :
 +* https://developers.redhat.com/articles/2023/04/24/how-debug-openshift-operators-live-cluster-using-dlv
 +* https://github.com/go-delve/delve/blob/master/Documentation/cli/getting_started.md
 +
 +
 +
 +## Jupyter Notebook
 +
 +Voir : https://github.com/gopherdata/gophernotes#linux-or-freebsd
 +
 +
 +## Ordiphone / Android / téléphone mobile
 +
 +Voir :
 +* https://github.com/golang/go/wiki/Mobile
 +* https://betterprogramming.pub/build-native-cross-platform-apps-with-go-70f9572baeb5
 +* https://rogchap.com/2020/09/14/running-go-code-on-ios-and-android/
 +
 +Voir GoMobile :
 +* https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile
 +
 +Voir Gioui :
 +* https://gioui.org/
 +* https://www.gushiciku.cn/pl/pkdc
 +
 +Voir Fyne :
 +* https://fyne.io/
 +* https://www.youtube.com/watch?v=S3T9l9QUa9I
 +
 +~~~bash
 +go get fyne.io/fyne/cmd/fyne
 +fyne package -os android -appID xyz.acme.plop
 +~~~
 +
 +Tuto Fyne notes
 +* https://www.youtube.com/watch?v=ZYtd6rjP0TM
 +* https://github.com/fynelabs/notes
 +
 +Voir aussi
 +https://www.youtube.com/watch?v=jbsYrrNiqAs
 +
 +
 +## Limitations
 +
 +
 +no unset
 +Pas d'erreur lors de l'appel d'une variable non définie (mais initialisée) 
 +~~~go
 +var num int
 +
 +fmt.Println(num)
 +~~~
 +
 +Overflow
 +Voir :
 +* https://cwe.mitre.org/data/definitions/190.html
 + 
 +~~~go
 +var num uint32 = 1 <<32 - 1
 +
 +fmt.Println(num, num+1, num < num+1)
 +// 4294967295 0 false
 +~~~
 +
 +Voir aussi :
 +* go-safecast
 +* https://pkg.go.dev/github.com/mrtkp9993/go-overflow
 +* https://blog.rene.sh/blog/2020/06/22/int-overflow/
 +
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki