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.

D8e5d79ca42edc07693b9c1aacaa7e5e?s=128

Francesc Campoy Flores

October 10, 2014
Tweet

Transcript

  1. Things I learned teaching Go Francesc Campoy

  2. Adoption

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

    resources - the Go tour - blog posts - documentation - talks
  4. Learning a new language

  5. I caught a cold

  6. estoy constipado

  7. je suis … I am ...

  8. False Friends

  9. Java iterating over a collection for (Thing thing: things) Go

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

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

    iterating over a slice for _, thing := range things
  12. None
  13. “Like Yoda speaks code you must not”

  14. Idiomatic Go code

  15. Foreign influence

  16. type Vertex struct { x, y int } func (this

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

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

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

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

    *Vertex) Abs() float64 { x, y := v.x, v.y return math.Sqrt(x*x + y*y) }
  23. None
  24. Things that are hard to learn

  25. Eyjafjallajökull

  26. None
  27. Interfaces

  28. type Stringer interface { String() string }

  29. Set of constraints on types

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

    { String() string }
  31. type Reader interface { Read([]byte) (int, error) } type Writer

    interface { Write([]byte) (int, error) }
  32. type ReadWriter interface { Reader Writer }

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

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

  35. Writer ReadWriter ??? Reader

  36. interface{}

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

  38. Method sets

  39. 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
  40. 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
  41. 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
  42. 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) }
  43. 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) }
  44. Hillary is 66 years old

  45. 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) }
  46. Hillary is 66 years old

  47. 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
  48. 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) }
  49. Hillary is 66 years old

  50. 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) }
  51. {Hillary 66}

  52. func (T) Foo() func (*T) Foo()

  53. func (T) Foo() func (*T) Foo() T → T *T

    → *T
  54. func (T) Foo() func (*T) Foo() T → T *T

    → *T *T → T
  55. func (T) Foo() func (*T) Foo() T → T *T

    → *T *T → T T → *T
  56. &42

  57. func (T) Foo() func (*T) Foo() T → T *T

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

    Foo()
  59. None
  60. nil

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

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

    == nil) fmt.Println(s, s == nil) # <nil> true # <nil> false
  63. (type, value)

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

    # (*main.Person)(nil)
  65. (*Person, nil) doesn’t equal nil

  66. var s fmt.Stringer fmt.Printf(“%#v”, s) # <nil>

  67. (nil, nil) equals nil

  68. When nil is not nil

  69. func do() error { var err *doError … return err

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

    } func main() { if err := do(); err != nil { fmt.Println(err) } }
  71. Other hard things

  72. How do I learn Go?

  73. read the specs often, really Know the basics

  74. write code Immersion

  75. share your code and ask for feedback Immersion

  76. false friends and idioms Don’t be embarrassed

  77. Always pass on what you have learned - Yoda

  78. Adoption

  79. @francesc Merci

  80. - 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