Slide 1

Slide 1 text

Things I learned teaching Go Francesc Campoy

Slide 2

Slide 2 text

Adoption

Slide 3

Slide 3 text

Making Go easy to learn - live workshops - online resources - the Go tour - blog posts - documentation - talks

Slide 4

Slide 4 text

Learning a new language

Slide 5

Slide 5 text

I caught a cold

Slide 6

Slide 6 text

estoy constipado

Slide 7

Slide 7 text

je suis … I am ...

Slide 8

Slide 8 text

False Friends

Slide 9

Slide 9 text

Java iterating over a collection for (Thing thing: things) Go iterating over a slice for thing := range things

Slide 10

Slide 10 text

Java iterating over a collection for (Thing thing: things) Go iterating over a slice for thing := range things

Slide 11

Slide 11 text

Java iterating over a collection for (Thing thing: things) Go iterating over a slice for _, thing := range things

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“Like Yoda speaks code you must not”

Slide 14

Slide 14 text

Idiomatic Go code

Slide 15

Slide 15 text

Foreign influence

Slide 16

Slide 16 text

type Vertex struct { x, y int } func (this *Vertex) Abs() float64 { x, y := this.GetX(), this.GetY() return math.Sqrt(x*x + y*y) }

Slide 17

Slide 17 text

type Vertex struct { x, y int } func (this *Vertex) Abs() float64 { x, y := this.GetX(), this.GetY() return math.Sqrt(x*x + y*y) }

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

type Vertex struct { _x, _y int } func (self *Vertex) Abs() float64 { x, y := self._x, self._y return math.Sqrt(x*x + y*y) }

Slide 20

Slide 20 text

type Vertex struct { _x, _y int } func (self *Vertex) Abs() float64 { x, y := self._x, self._y return math.Sqrt(x*x + y*y) }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

type Vertex struct { x, y int } func (v *Vertex) Abs() float64 { x, y := v.x, v.y return math.Sqrt(x*x + y*y) }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Things that are hard to learn

Slide 25

Slide 25 text

Eyjafjallajökull

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Interfaces

Slide 28

Slide 28 text

type Stringer interface { String() string }

Slide 29

Slide 29 text

Set of constraints on types

Slide 30

Slide 30 text

type Stringer interface { String() string } type myCoolStringer interface { String() string }

Slide 31

Slide 31 text

type Reader interface { Read([]byte) (int, error) } type Writer interface { Write([]byte) (int, error) }

Slide 32

Slide 32 text

type ReadWriter interface { Reader Writer }

Slide 33

Slide 33 text

Reader gzip.Reader rand.Reader ReadWriter *os.File net.Conn

Slide 34

Slide 34 text

Reader gzip.Reader rand.Reader Writer gzip.Writer log.Writer ReadWriter *os.File net.Conn

Slide 35

Slide 35 text

Writer ReadWriter ??? Reader

Slide 36

Slide 36 text

interface{}

Slide 37

Slide 37 text

method set (I) ⊆ method set (T) Implicit satisfaction

Slide 38

Slide 38 text

Method sets

Slide 39

Slide 39 text

The method set of any other type T consists of all methods declared with receiver type T. The method set of a pointer type *T is the set of all methods declared with receiver *T or T. The method set of an interface type is its interface

Slide 40

Slide 40 text

The method set of any other type T consists of all methods declared with receiver type T. The method set of a pointer type *T is the set of all methods declared with receiver *T or T. The method set of an interface type is its interface

Slide 41

Slide 41 text

The method set of any other type T consists of all methods declared with receiver type T. The method set of a pointer type *T is the set of all methods declared with receiver *T or T. The method set of an interface type is its interface

Slide 42

Slide 42 text

type Person struct { Name string Age int } func (p *Person) String() string { return fmt.Sprintf("%s is %d", p.Name, p.Age) } func main() { p := &Person{"Hillary", 66} fmt.Println(p) }

Slide 43

Slide 43 text

type Person struct { Name string Age int } func (p *Person) String() string { return fmt.Sprintf("%s is %d", p.Name, p.Age) } func main() { p := &Person{"Hillary", 66} fmt.Println(p) }

Slide 44

Slide 44 text

Hillary is 66 years old

Slide 45

Slide 45 text

type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s is %d", p.Name, p.Age) } func main() { p := &Person{"Hillary", 66} fmt.Println(p) }

Slide 46

Slide 46 text

Hillary is 66 years old

Slide 47

Slide 47 text

The method set of any other type T consists of all methods declared with receiver type T. The method set of a pointer type *T is the set of all methods declared with receiver *T or T. The method set of an interface type is its interface

Slide 48

Slide 48 text

type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s is %d", p.Name, p.Age) } func main() { p := Person{"Hillary", 66} fmt.Println(p) }

Slide 49

Slide 49 text

Hillary is 66 years old

Slide 50

Slide 50 text

type Person struct { Name string Age int } func (p *Person) String() string { return fmt.Sprintf("%s is %d", p.Name, p.Age) } func main() { p := Person{"Hillary", 66} fmt.Println(p) }

Slide 51

Slide 51 text

{Hillary 66}

Slide 52

Slide 52 text

func (T) Foo() func (*T) Foo()

Slide 53

Slide 53 text

func (T) Foo() func (*T) Foo() T → T *T → *T

Slide 54

Slide 54 text

func (T) Foo() func (*T) Foo() T → T *T → *T *T → T

Slide 55

Slide 55 text

func (T) Foo() func (*T) Foo() T → T *T → *T *T → T T → *T

Slide 56

Slide 56 text

&42

Slide 57

Slide 57 text

func (T) Foo() func (*T) Foo() T → T *T → *T *T → T T ↛ *T

Slide 58

Slide 58 text

*T T func (T) Foo() func (T) Foo() func (*T) Foo()

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

nil

Slide 61

Slide 61 text

var s fmt.Stringer fmt.Println(s, s == nil) # , true

Slide 62

Slide 62 text

var p *Person var s fmt.Stringer = p fmt.Println(p, p == nil) fmt.Println(s, s == nil) # true # false

Slide 63

Slide 63 text

(type, value)

Slide 64

Slide 64 text

var p *Person var s fmt.Stringer = p fmt.Printf(“%#v”, s) # (*main.Person)(nil)

Slide 65

Slide 65 text

(*Person, nil) doesn’t equal nil

Slide 66

Slide 66 text

var s fmt.Stringer fmt.Printf(“%#v”, s) #

Slide 67

Slide 67 text

(nil, nil) equals nil

Slide 68

Slide 68 text

When nil is not nil

Slide 69

Slide 69 text

func do() error { var err *doError … return err } func main() { if err := do(); err != nil { fmt.Println(err) } }

Slide 70

Slide 70 text

func do() error { var err *doError … return err } func main() { if err := do(); err != nil { fmt.Println(err) } }

Slide 71

Slide 71 text

Other hard things

Slide 72

Slide 72 text

How do I learn Go?

Slide 73

Slide 73 text

read the specs often, really Know the basics

Slide 74

Slide 74 text

write code Immersion

Slide 75

Slide 75 text

share your code and ask for feedback Immersion

Slide 76

Slide 76 text

false friends and idioms Don’t be embarrassed

Slide 77

Slide 77 text

Always pass on what you have learned - Yoda

Slide 78

Slide 78 text

Adoption

Slide 79

Slide 79 text

@francesc Merci

Slide 80

Slide 80 text

- Eyjafjallajökull By Boaworm (Own work) CC-BY-3.0 via Wikimedia Commons - Yoda picture “Save you it can.” by JD Hancock is licensed under a Creative Commons Attribution 3.0 - Chinese letters by Michael Coghlan is licensed under a Creative Commons Attribution 3.0 Attributions