Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go go go !

Go go go !

A short introduction to golang language.

Sébastien Cogneau

December 08, 2016
Tweet

Other Decks in Programming

Transcript

  1. Généralités  Créé chez Google par Robert Griesmer,Rob Pike et

    Ken Thompson en 2007  V1.0 le 28 mars 2012  Statiquement typé  Garbage collecté
  2. Objectifs  Performant  Accessible  Exploiter au mieux le

    matériel https://talks.golang.org/2012/splash.article
  3. Package  Encapsulation  Initialisation  Le nom du package

    correspond à l’url de téléchargement  github.com/scogneau/softshake2016/message
  4. Fonctions  First Class Citizen  Peuvent retourner plusieurs valeurs

    func call(f func(i int) string){ i:=12 fmt.Fprintf(os.Stdout,"Appel de f(%d) retourne %s\n",i,f(i)) } func main() { cast := func(i int)string{ return strconv.Itoa(i) } x2 := func(i int)string { return strconv.Itoa(i*2) } call(cast) call(x2) }
  5. Defer  Stack les appels de fonctions pour les exécuter

    à la fin func main() { for i:=0;i<10;i++{ defer fmt.Println(i) } fmt.Println("Attention, c'est parti.") }
  6. Structure  Reçoit les méthodes type point struct { x,y

    int } type Cercle struct { point rayon float64 } func (c Cercle) perimetre() float64 { return 2*c.rayon*math.Pi } func main() { c := Cercle{point{x:2,y:9},12} fmt.Fprintf(os.Stdout,"Cercle de centre (%d,%d) et de rayon %f perimetre %f\n",c.x,c.y,c.rayon,c.perimetre()) }
  7. Interface  Groupe un ensemble de comportement  Pas de

    lien explicite entre un type et une interface type Cercle struct { point rayon float64 } func (c Cercle) perimetre() float64 { return 2*c.rayon*math.Pi } type Figure interface { perimetre() float64 }
  8. Panic  Erreur « imprévues »  Execute toutes les

    deferred fonctions avant de quitter  Affiche les traces de toutes les goroutines  Quitte le programme
  9. Go routine  Activités qui s’executent en parallèle  Plusieurs

    routine peuvent s’exécuter dans un même thread système
  10. Channel  Permet la communication entre plusieurs goroutines  Multiplexage

    possible x := make(chan(int),1) x <-1 fmt.Println(<-x)
  11. Structure du workspace  $GOPATH pour définir l’emplacement  Bin

    → les exécutables  Pkg → les packages utilisés  Src → Les sources
  12. Vendoring  Gestion des dépendances  Nuts  Godep 

    Party  Support standard  Copie dans le répertoire du projet avec gomvpkg
  13. Outil de base  Go build  Go run 

    Go fmt  Go lint  Go fix  Go doc
  14. Qualité  Go tool vet permet de faire une analyse

    des sources  Go doc -analysis type  Golint vérifie le formatage  Defercheck  Varcheck  Structcheck