3. LE LANGAGE GO
Créé en 2009 par Google
(Robert Griesemer, Rob Pike et Ken Thompson)
Inspiré du C et de Pascal
Langage compilé
Récupérateur de mémoire (garbage-collected)
4. OSCON 2010
Go aims to combine the safety and performance of a statically typed
compiled language with the expressiveness and convenience of a
dynamically typed interpreted language.
It also aims to be suitable for modern systems - large scale -
programming.
-- Rob Pike
5. GO(LANG) EXEMPLE
package main
import "fmt"
func main() {
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
fmt.Println("unsorted:", list)
bubblesort(list)
fmt.Println("sorted! ", list)
}
func bubblesort(a []int) {
for itemCount := len(a) - 1; ; itemCount-- {
hasChanged := false
for index := 0; index < itemCount; index++ {
6. LE LANGAGE GO!
Créé en 2003 par Keith Clark et Francis McCabe.
Programmation concurrentielle
7. GO! - EXEMPLE
Gender::= male | female.
person <˜ {dayOfBirth:[]=>day. age:[]=>integer.
gender:[]=>Gender. name:[]=>string.
home:[]=>string. lives:[string]{}}.
person:[string,day,Gender,string]$=person.
person(Nm,Born,Sx,Hm)..{
dayOfBirth()=>Born.
age() => yearsBetween(now(),Born).
gender()=>Sx.
name()=>Nm.
home()=>Hm.
lives(Pl) :- Pl=home().
yearsBetween:[integer day]=>integer
11. POURQUOI GO ?
La simplicité
Les interfaces
Les goroutines / channels
La toolchain
La librairie standard
Rapidité de compilation
12. GO UN LANGAGE SIMPLE
package main
import "fmt"
func main() {
// L'Unicode ça doit être simple...
fmt.Println("Hello 世界")
}
fmt Documentation
13. UNE SEULE BOUCLE
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
for ; sum > 0; {
sum -= 1
}
for sum < 1000 {
sum += 1
}
14. TYPES DE BASE
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias pour uint8
rune // alias pour int32
// représente un "code point" Unicode
float32 float64
complex64 complex128
15. DES TYPES COMPOSÉS
package main
import "fmt"
type Sommet struct {
X int
Y int
}
func main() {
v := Sommet{1, 2}
v.X = 4
fmt.Println(v.X)
}
16. DES SLICES (ET DES TABLEAUX)
// Array : taille fixe
var a [10]int
// Slice : taille variable
var b[]int
17. DES SLICES
// Des slices de slices
game := [][]string{
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
}
// Morceau de slice retourne un slice
game[1:3] // ou game[1:]
// Ajout
game = append(game, []string{" ", " "," "});
// Boucle for avec range
for i v := range game {
18. DES MAPS
type Sommet struct {
Lat, Long float64
}
var m map[string]Sommet
func main() {
m = make(map[string]Sommet)
m["Bell Labs"] = Sommet{
40.68433, -74.39967,
}
fmt.Println(m["Bell Labs"])
}
19. DES MAPS
// Affectation
m[key] = elem
// Récupération
elem = m[key]
// Suppression
delete(m, key)
// Test existence
elem, ok = m[key] // _, ok = m[key]
22. POO EN GO ?
Go pas d'héritage (interface)
Pas de classe (struct)
Polymorphisme
23. INTERFACES
Pour principe de substitution de Liskov
(pas d'héritage)
Implicites
(pas besoin de modi er les packages externes)
Peuvent être déroutantes
24. STRUCT +FONCTION =>MÉTHODE
type Sommet struct {
X, Y float64
}
func (s Sommet) Abs() float64 {
return math.Sqrt(s.X*s.X + s.Y*s.Y)
}
25. INTERFACE =LISTE DE MÉTHODES
package main
import (
"fmt"
"math"
)
type cercle struct {
rayon float64
}
func (c cercle) aire() float64 {
return math.Pi * c.rayon * c.rayon
}
func (c cercle) perimetre() float64 {
26. GOROUTINES
Plus légers que les thread
Usage de la stack optimisé
Switchs peu couteux
Concurrency != parallelism
27. GOROUTINES
Des millions de goroutines possible
50 fois plus rapide à lancer qu'un thread
2-4K vs 2M sur la stack
Changement de contexte peu couteux
Un thread système gère plusieurs goroutines
28. GOROUTINE - EXEMPLE
package main
import (
"fmt"
"time"
)
func longTraitement(d int, s string) {
time.Sleep(time.Duration(d) * time.Second)
fmt.Println(s)
}
func main() {
go longTraitement(3 "Premier long Traitement")
30. CHANNELS
Vont de paire avec les goroutines
First class elements
Bufferisés ou pas
Pas de callback !
31. CHANNELS
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
34. TEMPS DE COMPILATION
Analyse plus simple
Pas de dépendances inutilisées
Pas de dépendances cycliques
Syntaxe régulière
Un import => un chier à charger
Format objet avec les exports en début de chier
35. LIBRAIRIE STANDARDBIEN FOURNIE
Liste des
- Index et moteur de recherche des packages
- Moteur de recherche de code
packages standards
GoDoc
Go Search
And github...
47. CREDITS
pour l'idée du Gopher sur les
slides
pour le bonheur de ne plus utiliser
PowerPoint
Vous êtes libre de modi er/copier/redistribuer librement cette présentation.
kljensen/go-is-awesome
Reveal.js