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

Things I learned teaching Go

Things I learned teaching Go

Some thoughts about the difference between learning natural languages and programming languages, false friends, and hard stuff about interfaces.

Francesc Campoy Flores

October 10, 2014
Tweet

More Decks by Francesc Campoy Flores

Other Decks in Programming

Transcript

  1. Making Go easy to learn - live workshops - online

    resources - the Go tour - blog posts - documentation - talks
  2. Java iterating over a collection for (Thing thing: things) Go

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

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

    iterating over a slice for _, thing := range things
  5. type Vertex struct { x, y int } func (this

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

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

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

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

    *Vertex) Abs() float64 { x, y := v.x, v.y return math.Sqrt(x*x + y*y) }
  10. type Reader interface { Read([]byte) (int, error) } type Writer

    interface { Write([]byte) (int, error) }
  11. 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
  12. 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
  13. 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
  14. 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) }
  15. 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) }
  16. 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) }
  17. 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
  18. 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) }
  19. 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) }
  20. &42

  21. nil

  22. var p *Person var s fmt.Stringer = p fmt.Println(p, p

    == nil) fmt.Println(s, s == nil) # <nil> true # <nil> false
  23. func do() error { var err *doError … return err

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

    } func main() { if err := do(); err != nil { fmt.Println(err) } }
  25. - 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