Slide 1

Slide 1 text

gophers! Ben Lovell

Slide 2

Slide 2 text

benlovell _j

Slide 3

Slide 3 text

113581334398839860922 (thanks, google)

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

sometimes we don’t get along…

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

what is go? … and why does it exist

Slide 10

Slide 10 text

go will be the server language of the future - Tobias Lütke, Shopify founder

Slide 11

Slide 11 text

go is fast

Slide 12

Slide 12 text

go is object-oriented (but not as you know it)

Slide 13

Slide 13 text

go is statically typed

Slide 14

Slide 14 text

go is garbage collected

Slide 15

Slide 15 text

go is concurrent

Slide 16

Slide 16 text

optimised for developer happiness

Slide 17

Slide 17 text

a brief history of go

Slide 18

Slide 18 text

started in 2007 by three googlers…

Slide 19

Slide 19 text

Rob Pike

Slide 20

Slide 20 text

Ken Thompson

Slide 21

Slide 21 text

Robert Griesemer

Slide 22

Slide 22 text

open sourced in 2009 (over 300 contributors to date)

Slide 23

Slide 23 text

1.0 release March 2012

Slide 24

Slide 24 text

what was left out is more important than what was put in - UNIX philosophy

Slide 25

Slide 25 text

hello, world!

Slide 26

Slide 26 text

package main ! import "fmt" ! func main() { fmt.Println("Hello, world!") }

Slide 27

Slide 27 text

the type system

Slide 28

Slide 28 text

package main ! import "fmt" ! type Person string ! func (person Person) Greet() { fmt.Printf("Hello, %s!\n", person) } ! func main() { person := Person("Ben") person.Greet() } !

Slide 29

Slide 29 text

package main ! import "fmt" ! type Person struct { Name string Age int } ! func (person Person) Greet() { fmt.Println(person.Name) } ! func main() { person := Person{"Ben", 33} person.Greet() } !

Slide 30

Slide 30 text

no classes…

Slide 31

Slide 31 text

…no type hierarchy

Slide 32

Slide 32 text

favour composition over inheritance

Slide 33

Slide 33 text

arranging code in packages

Slide 34

Slide 34 text

% export GOPATH=/gocode/ ! /gocode /gocode/src

Slide 35

Slide 35 text

package person ! import "fmt" ! type Person struct { Name string } ! func (person Person) Greet() string { return fmt.Sprintf("Hello, %s", person.Name) } ~/gocode/src/person/person.go

Slide 36

Slide 36 text

~/gocode/src/main.go ~/gocode/src % go run main.go package main ! import ( "fmt" "person" ) ! func main() { person := person.Person{"Ben"} greeting := person.Greet() fmt.Println(greeting) } !

Slide 37

Slide 37 text

visibility?

Slide 38

Slide 38 text

start your identifier with a lowercase letter to make it private

Slide 39

Slide 39 text

function arguments are copied

Slide 40

Slide 40 text

package main ! import ( "fmt" "strings" ) ! func Upcase(name string) { name = strings.ToUpper(name) } ! func main() { name := "ben" Upcase(name) ! fmt.Println(name) } ! // ben

Slide 41

Slide 41 text

pointers! (scream)

Slide 42

Slide 42 text

// BEN package main ! import ( "fmt" "strings" ) ! func Upcase(name *string) { *name = strings.ToUpper(*name) } ! func main() { name := "ben" Upcase(&name) ! fmt.Println(name) } !

Slide 43

Slide 43 text

the same applies to function receivers

Slide 44

Slide 44 text

package main ! import "fmt" ! type Person struct { Name string } ! func (p Person) SetName(name string) { p.Name = name } ! func main() { p := Person{"Ben"} p.SetName("Jim") fmt.Println(p.Name) } ! // Ben

Slide 45

Slide 45 text

// Jim package main ! import "fmt" ! type Person struct { Name string } ! func (p *Person) SetName(name string) { p.Name = name } ! func main() { p := Person{"Ben"} p.SetName("Jim") fmt.Println(p.Name) } !

Slide 46

Slide 46 text

pointers are not just for reference semantics

Slide 47

Slide 47 text

pointers are not just for reference semantics Christmas

Slide 48

Slide 48 text

representing is-a with type embedding (well, kinda)

Slide 49

Slide 49 text

// I’M BEN // I’m Ben type Person struct { Name string } ! func (p Person) Greet() { fmt.Println("I'm", p.Name) } ! type Manager struct { Person } ! func (m Manager) Greet() { fmt.Println("I'M", strings.ToUpper(m.Name)) } ! func main() { m := Manager{Person{"Ben"}} m.Greet() m.Person.Greet() }

Slide 50

Slide 50 text

signature based polymorphism (quack quack)

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

interfaces

Slide 53

Slide 53 text

type Person struct { Name string } ! func (p Person) Greet() { fmt.Println("I'm", p.Name) } ! type Manager struct { Person } ! func (m Manager) Greet() { fmt.Println("I'M", strings.ToUpper(m.Name)) } ! func greet(greeters ...Greeter) { for _, g := range greeters { g.Greet() } } ! type Greeter interface { Greet() } ! func main() { greet(Person{"Ben"}, Manager{Person{"Bob"}}, Person{"Anne"}) }

Slide 54

Slide 54 text

interfaces are implicitly satisfied

Slide 55

Slide 55 text

querying for support and casting

Slide 56

Slide 56 text

func greet(greeters ...interface{}) { for _, g := range greeters { g.(Greeter).Greet() } }

Slide 57

Slide 57 text

don’t. there are safe idioms

Slide 58

Slide 58 text

func greet(greeters ...interface{}) { for _, g := range greeters { greetable, ok := g.(Greeter) if ok { greetable.Greet() } else { fmt.Println("Whoops!") } } } ! func main() { greet(Person{"Ben"}, "WHOA!", Person{"Anne"}) } ! // I’m Ben // Whoops! // I’m Anne

Slide 59

Slide 59 text

error handling (the error interface!)

Slide 60

Slide 60 text

type error interface { Error() string }

Slide 61

Slide 61 text

// os.Open signature func Open(name string) (file *File, err error) ! ! f, err := os.Open("file.txt") if err != nil { log.Fatal(err) } // do something with the open file

Slide 62

Slide 62 text

f, err := os.Open("file.txt") if err != nil { log.Fatal(err) } ! defer f.Close() // do something with the *File f

Slide 63

Slide 63 text

concurrency (kind hard of is it)

Slide 64

Slide 64 text

communicating sequential processes

Slide 65

Slide 65 text

don’t communicate by sharing memory share memory by communicating

Slide 66

Slide 66 text

three simple constructs

Slide 67

Slide 67 text

go routines (lightweight threads)

Slide 68

Slide 68 text

func main() { go greet() ! // hang around a while time.Sleep(2 * time.Second) } ! func greet() { fmt.Println("Hi!") }

Slide 69

Slide 69 text

channels (for communication)

Slide 70

Slide 70 text

//make a channel c := make(chan int) ! //send on the channel c <- 1 ! //receive from the channel val := <- c

Slide 71

Slide 71 text

func ping(c chan string) { for i := 0; ; i++ { c <- "ping" } } ! func printer(c chan string) { for { msg := <- c fmt.Println(msg) time.Sleep(time.Second * 1) } } ! func main() { c := make(chan string) ! go ping(c) go printer(c) ! time.Sleep(time.Second * 10) }

Slide 72

Slide 72 text

func main() { s := make(chan int) ! go greet(s) s <- 1 } ! func greet(signaller chan int) { <- signaller fmt.Println("Hi!") }

Slide 73

Slide 73 text

func main() { functions := make(chan func(int, int)(int)) go executor(functions) ! functions <- add functions <- multiply functions <- subtract } ! func add(x, y int) int { return x + y } ! func multiply(x, y int) int { return x * y } ! func subtract(x, y int) int { return x - y } ! func executor(functions chan func(int, int)(int)) { for f := range functions { fmt.Println("The result is: ", f(10, 10)) } }

Slide 74

Slide 74 text

func main() { functions := make(chan func(int, int)(int)) go executor(functions) ! functions <- add functions <- multiply functions <- subtract } ! func add(x, y int) int { return x + y } ! func multiply(x, y int) int { return x * y } ! func subtract(x, y int) int { return x - y } ! func executor(functions chan func(int, int)(int)) { for f := range functions { fmt.Println("The result is: ", f(10, 10)) } } The result is: 20 The result is: 100 The result is: 0

Slide 75

Slide 75 text

select

Slide 76

Slide 76 text

func printer(c chan string, d chan string) { for { select { case x := <- c: fmt.Println(x) case y := <- d: fmt.Println(y) case <- time.After(1 * time.Second): fmt.Println("Timeout!") } } } ! func main() { c := make(chan string) d := make(chan string) ! go ping(c) go ping(d) go printer(c, d) ! time.Sleep(time.Second * 10) }

Slide 77

Slide 77 text

toolchain

Slide 78

Slide 78 text

% go run

Slide 79

Slide 79 text

% go fix

Slide 80

Slide 80 text

% go run main.go —race

Slide 81

Slide 81 text

% go fmt

Slide 82

Slide 82 text

% go get

Slide 83

Slide 83 text

% go doc

Slide 84

Slide 84 text

% go test

Slide 85

Slide 85 text

what I didn’t cover…

Slide 86

Slide 86 text

thanks! @benlovell