Go: the cool parts

Go: the cool parts

An introductory talk to Go where we will discuss why Go's popularity is raising so fast.

Presented at Øredev: http://oredev.org/2015/sessions/go-the-cool-parts

D8e5d79ca42edc07693b9c1aacaa7e5e?s=128

Francesc Campoy Flores

November 04, 2015
Tweet

Transcript

  1. Go: the cool parts Francesc Campoy @francesc

  2. Francesc Campoy Developer Advocate Google Cloud Platform Go On the

    internets: @francesc github.com/campoy
  3. The cool parts 1. The language 2. The tooling 3.

    The community
  4. The language

  5. Go is - open source - statically typed - garbage

    collected - simple - fast - cool - awesome - …
  6. A bit of history

  7. Scale axis Number of: cores concurrency machines networking lines of

    code compilation speed / maintainability developers readability
  8. Problems at scale

  9. Simplicity scales • strings and weights • gravity

  10. Simplicity scales • strings and weights • gravity + a

    new perspective
  11. Simplicity scales Modeling complexity: - small elements - simple rules

    - composition
  12. 1. The language a. the syntax b. the type system

    c. the concurrency support 2. The tooling 3. The community The cool parts
  13. The syntax

  14. Hello, world package main import "fmt" func main() { msg

    := "Hello, Øredev" fmt.Println(msg) }
  15. package main import "fmt" func main() { msg := "Hello,

    Øredev" fmt.Println(msg) } Hello, world
  16. package main import "fmt" func main() { msg := "Hello,

    Øredev" fmt.Println(msg) } Hello, world
  17. package main import "fmt" func main() { msg := "Hello,

    Øredev" fmt.Println(msg) } Hello, world
  18. func sum(a, b int) int { return a + b

    } Hello, functions
  19. Lambdas - Simply known as function literals - Functions are

    first class objects
  20. Hello, lambda func equals(a int) func(int) bool { return func(x

    int) bool { return a == x } }
  21. Hello, lambda func equals(a int) func(int) bool { return func(x

    int) bool { return a == x } }
  22. Hello, lambda is42 := equals(42) fmt.Println(is42(42)) // true fmt.Println(is42(10)) //

    false
  23. Closures - Function literals - Lexical scoping - Automatic stack/heap

    allocation
  24. func fib() func() int { a, b := 0, 1

    return func() int { a, b = b, a+b return a } } Hello, fibonacci
  25. Hello, fibonacci func fib() func() int { a, b :=

    0, 1 return func() int { a, b = b, a+b return a } }
  26. Hello, fibonacci func fib() func() int { a, b :=

    0, 1 return func() int { a, b = b, a+b return a } }
  27. The type system

  28. There’s two classes of types: - Concrete types - Interfaces

    Go types
  29. Describe memory layout Every value is of a concrete type

    Concrete types can be: - simple (predefined) or composed - named or anonymous Concrete types
  30. var ( num int // an integer ) Some concrete

    types
  31. var ( num int // an integer tenNums [10]int //

    an array of ten integers ) Some concrete types
  32. var ( num int // an integer tenNums [10]int //

    an array of ten integers nums []int // a slice of any number of ints ) Some concrete types
  33. var ( num int // an integer tenNums [10]int //

    an array of ten integers nums []int // a slice of any number of ints names map[int]string // a map from integer to strings ) Some concrete types
  34. var ( num int // an integer tenNums [10]int //

    an array of ten integers nums []int // a slice of any number of ints names map[int]string // a map from integer to strings ch chan int // a channel of integers ) Some concrete types: anonymous types
  35. // a function that given a float returns a float

    var mathFunc func(float64) float64 Some concrete types: anonymous types
  36. // a function that given a float returns a float

    var mathFunc func(float64) float64 // a struct with two fields: name and age var john struct { name string age int } Some concrete types: anonymous types
  37. Definition of named types: type XXX def Type conversion between

    concrete types is always explicit Named types
  38. type Celsius float64 Some concrete types: named types

  39. Some concrete types: named types type Celsius float64 type Fahrenheit

    float64
  40. Some concrete types: named types type Celsius float64 type Fahrenheit

    float64 c := Celsius(100) f := Fahrenheit(100) fmt.Println(c + f)
  41. Some concrete types: named types type Celsius float64 type Fahrenheit

    float64 c := Celsius(100) f := Fahrenheit(100) fmt.Println(c + f) invalid operation: c + f (mismatched types Celsius and Fahrenheit)
  42. Some concrete types: named types type Person struct { Name

    string Age int }
  43. Methods can be defined on named types from the same

    package - on any type, not only structs - only the same package, so no monkey patching Methods
  44. Methods type Person struct { Name string Age int }

    func (p Person) IsMajor() bool { return p.Age >= 18 }
  45. More methods type Celsius float64 func (c Celsius) IsFreezing() bool

    { return c <= 0 } type Fahrenheit float64 func (f Fahrenheit) IsFreezing() bool { return f <= 32 }
  46. Like the interface you know: - set of methods -

    any concrete type can satisfy an interface Interfaces
  47. Interfaces (cont) But different: - implicit satisfaction (substructural typing) -

    think duck typing for compile time
  48. func AmIHappy(t Temperature) bool { return !t.IsFreezing() } type Temperature

    interface { IsFreezing() bool } Interfaces
  49. c := Celsius(20) fmt.Println(AmIHappy(c)) // true f := Farenheit(20) fmt.Println(AmIHappy(f))

    // false Interfaces
  50. Concurrency

  51. Go Concurrency Based on: - goroutines - channels

  52. Very lightweight threads: - 2KB of memory footprint - handled

    by the Go runtime (fast switching) Goroutines
  53. Concurrency func sleepAndTalk(d time.Duration, s string) { time.Sleep(d) fmt.Println(s) }

  54. func main() { sleepAndTalk(0*time.Second, "Hello") sleepAndTalk(1*time.Second, "Øredev!") sleepAndTalk(2*time.Second, "What's") sleepAndTalk(3*time.Second,

    "up?") } Concurrency http://play.golang.org/p/i9GmPNrokI
  55. func main() { go sleepAndTalk(0*time.Second, "Hello") go sleepAndTalk(1*time.Second, "Øredev!") go

    sleepAndTalk(2*time.Second, "What's") go sleepAndTalk(3*time.Second, "up?") } Concurrency http://play.golang.org/p/lH38iJdIVJ
  56. func main() { go sleepAndTalk(0*time.Second, "Hello") go sleepAndTalk(1*time.Second, "Øredev!") go

    sleepAndTalk(2*time.Second, "What's") go sleepAndTalk(3*time.Second, "up?") time.Sleep(4*time.Second) } Concurrency http://play.golang.org/p/aSutVsWwzB
  57. Communication between goroutines - synchronous by default - typed and

    concurrently safe Channels
  58. Concurrency func sleepAndTalk(d time.Duration, s string, c chan string) {

    time.Sleep(d) c <- s }
  59. c := make(chan string) go sleepAndTalk(0*time.Second, "Hello", c) go sleepAndTalk(1*time.Second,

    "Øredev!", c) go sleepAndTalk(2*time.Second, "What's", c) go sleepAndTalk(3*time.Second, "up?", c) for i := 0; i < 4; i++ { fmt.Println(<-c) } Concurrency http://play.golang.org/p/vF515iatfl
  60. Performance Generating 16Mpx: - sequentially - with wait groups -

    with channels github.com/campoy/mandelbrot
  61. Goroutine performance: mandelbrot 16MB

  62. The tooling

  63. go build: the Go compiler

  64. Written in Go since 1.5 - ~780k lines of code

    It is *very* fast - so fast you might feel it’s interpreted go build: the Go compiler
  65. Compiling the Go compiler ~780k lines of Go code How

    long will it take?
  66. None
  67. Generate binaries ready to deploy with scp … Static linking

    + cross compilation … for any platform!
  68. https://www.youtube.com/watch?v=w0610MoPv3g

  69. Come tomorrow for more on tooling! - code formatting -

    code analysis - debugging - etc. ANALYZING GO PROGRAMS: NO READING REQUIRED And many more tools
  70. The community

  71. A growing community

  72. Proposing Changes to Go An effort to make the community:

    - open (github.com/golang/proposal) - welcoming - transparent
  73. Code of Conduct One of the changes proposed! Based on

    the Django Code of Conduct The basics: Be friendly and welcoming Be patient Be respectful Be charitable Be thoughtful
  74. Conferences: - dotGo - Paris (next week!) - GopherCon -

    Denver, Dubai, and India - GoCon - Tokyo - GolangUK - many more Lively community
  75. Lively community And more: - Meetups - go-meetups.appspot.com - Women

    Who Go - GoBridge
  76. one more reason?

  77. We’re fancy

  78. Thanks! Francesc Campoy Google San Francisco campoy@golang.org @francesc