Golang

# Histoire
# Histoire

Bell Labs

Rob Pike

Google V8, JVM

Robert Griesemer

Bell Labs, B, Unix

Ken Thompson

# Histoire
# Histoire

Go gopher

# Caractéristiques
# Caractéristiques
# Caractéristiques
# Hello World
package main
# Hello World
package main

func main() {
}
# Hello World
package main

import "fmt"

func main() {
	fmt.Printf("Hello World!\n")
}
# Hello World
package main

import "fmt"

func main() {
	fmt.Printf("Hello World!\n")
}
$> go run main.go
Hello World!

$> go build main.go
$> ls
main main.go
$> ./main
Hello World!
# Variables
package main

import "fmt"

func main() {
    name := "Gopher"
    fmt.Printf("Hello %s\n", name)
}
package main

import "fmt"

func main() {
    var name string = "Gopher"
    fmt.Printf("Hello %s\n", name)
}
# Variables
package main

import "fmt"

func main() {
    var name string
    fmt.Printf("Hello world\n")
}
$> go run main.go
# command-line-arguments
./main.go:6:6: name declared but not used
# Variables
Boolean bool
Chaine de caractère string
Entiers int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64
uintptr
Data byte (uint8)
Unicode rune
Nombre non entier float32 float64
Nombre complexes complex64 complex128
# Conditions
package main

import "fmt"

func main() {
	health := 100
	if health <= 0 {
		fmt.Printf("You died\n")
	}
}
# Conditions
package main

import "fmt"

func main() {
	health := 100
	if health <= 0 {
		fmt.Printf("You died\n")
	} else {
		fmt.Printf("Keep going\n")
	}
}
# Conditions
package main

import "fmt"

func main() {
	if health := 100; health <= 0 {
		fmt.Printf("You died\n")
	} else {
		fmt.Printf("Keep going\n")
	}
}
# Conditions
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	s1 := rand.NewSource(time.Now().UnixNano())
	r1 := rand.New(s1)

	switch value := r1.Intn(10); value {
	case 5:
		fmt.Printf("You are in luck, 5!\n")
	case rand.Intn(10):
		fmt.Printf("You are in luck, same int twice in a row!\n")
	default:
		fmt.Printf("You lost %d\n", value)
	}
}
# Loops
# Loops
package main

import "fmt"

func main() {
	for i := 0; i < 10; i++ {
		fmt.Printf("%d\n", i)
	}
}
# Loops
package main

import (
	"fmt"
)

func main() {
	i := 0
	for i < 10 {
		fmt.Printf("%d\n", i)
		i++
	}
}
# Loops
package main

import (
	"fmt"
)

func main() {
	i := 0
	for {
		fmt.Printf("%d\n", i)
		i++
	}
}
# Loops
package main

import (
	"fmt"
)

func main() {
	users := [...]string{"Foo", "Bar"}

	for _, user := range users {
		fmt.Printf("%s\n", user)
	}
}
# Return values
package main

import (
	"fmt"
)

func foo() {
}

func main() {
	fmt.Printf("%s\n", foo())
}
# Return values
package main

import (
	"fmt"
)

func foo() {
	return "Bar"
}

func main() {
	fmt.Printf("%s\n", foo())
}
# Return values
package main

import (
	"fmt"
)

func foo() string {
	return "Bar"
}

func main() {
	fmt.Printf("%s\n", foo())
}
# Return values
package main

import (
	"fmt"
)

func foo(a int, b int) (total int) {
	total = a + b
	return
}

func main() {
	fmt.Printf("%d\n", foo(1, 4))
}
# Return values
# Return values
package main

import (
	"errors"
	"fmt"
)

func foo(a int, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("Unable to divide by 0")
	}
	return a / b, nil
}

func main() {
	result, err := foo(5, 2)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%d\n", result)
}
# Modules
// foo/bar.go
package main

import (
	"fmt"
)

func bar() {
	fmt.Printf("Hello World\n")
}
// main.go
package main

func main() {
}
# Modules
// foo/bar.go
package main

import (
	"fmt"
)

func bar() {
	fmt.Printf("Hello World\n")
}
// main.go
package main

import "demo/foo"

func main() {
	
}
# Modules
// foo/bar.go
package main

import (
	"fmt"
)

func bar() {
	fmt.Printf("Hello World\n")
}
// main.go
package main

import "demo/foo"

func main() {
	foo.bar()
}
$> go run main.go
# command-line-arguments
./main.go:6:6: undefined: foo.bar
# Modules
// foo/bar.go
package main

import (
	"fmt"
)

func Bar() {
	fmt.Printf("Hello World\n")
}
// main.go
package main

import "demo/foo"

func main() {
	foo.Bar()
}
$> go run main.go
Hello World
# Modules
// foo/bar.go
package main

import (
	"fmt"
)

var Baz string

func Bar() {
	fmt.Printf("Hello World\n")
}
// main.go
package main

import "demo/foo"

func main() {
	foo.Bar()
}
$> go run main.go
Hello World
# Structs
package main

type Character struct {
	health uint
}

func main() {
	
}
# Structs
package main

type Character struct {
	health int
}

func (character Character) isAlive() bool {
	return character.health > 0
}

func (character Character) damage(damages int) {
	character.health -= damages
}
 
func main() {
	
}
# Structs
package main

import "fmt"

type Character struct {
	health int
}

func (character Character) isAlive() bool {
	return character.health > 0
}

func (character Character) damage(damages int) {
	character.health -= damages
}

func main() {
	character := Character{health: 100}
	character.damage(50)
	fmt.Printf("Character have %d health points\n", character.health)
}
# Structs
package main

import "fmt"

type Character struct {
	health int
}

func (character Character) isAlive() bool {
	return character.health > 0
}

func (character *Character) damage(damages int) {
	character.health -= damages
}

func main() {
	character := Character{health: 100}
	character.damage(50)
	fmt.Printf("Character have %d health points\n", character.health)
}
# Defer
package main

import "fmt"

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}
$> go run main.go                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ─╯
hello
world
# Goroutines
package main

import (
	"fmt"
	"time"
)

func longRunningFunction(id int) {
	fmt.Printf("%d started\n", id)
	time.Sleep(1 * time.Second)
	fmt.Printf("%d finished!\n", id)
}

func main() {
	for i := 1; i < 10; i++ {
		longRunningFunction(i)
	}
}
# Goroutines
package main

import (
	"fmt"
	"time"
)

func longRunningFunction(id int) {
	fmt.Printf("%d started\n", id)
	time.Sleep(1 * time.Second)
	fmt.Printf("%d finished!\n", id)
}

func main() {
	for i := 1; i < 10; i++ {
		go longRunningFunction(i)
	}
	time.Sleep(2 * time.Second)
}
# Goroutines
package main

import (
	"math/rand"
	"time"
)

type result struct {
	id     int
	number int
}

func longRunningFunction(id int) result {
	time.Sleep(1 * time.Second)
	return result{id: id, number: rand.Int()}
}

func main() {
	for i := 1; i < 10; i++ {
		go longRunningFunction(i)
	}
	time.Sleep(2 * time.Second)
}
# Goroutines
package main

import (
	"math/rand"
	"time"
)

type result struct {
	id     int
	number int
}

func longRunningFunction(id int, channel chan result) result {
	time.Sleep(1 * time.Second)
	return result{id: id, number: rand.Int()}
}

func main() {
	resultChannel := make(chan result)
	for i := 1; i < 10; i++ {
		go longRunningFunction(i, resultChannel)
	}
	time.Sleep(2 * time.Second)
}
# Goroutines
package main

import (
	"fmt"
	"math/rand"
	"time"
)

type result struct {
	id     int
	number int
}

func longRunningFunction(id int, channel chan result) {
	time.Sleep(1 * time.Second)
	channel <- result{id: id, number: rand.Int()}
}

func main() {
	resultChannel := make(chan result)
	for i := 1; i < 10; i++ {
		go longRunningFunction(i, resultChannel)
	}
	for i := 1; i < 10; i++ {
		result := <-resultChannel
		fmt.Printf("%d rolled %d\n", result.id, result.number)
	}
}
# Goroutines
# Goroutines
Made with Slides.com