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

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

Francesc Campoy Flores

November 04, 2015
Tweet

More Decks by Francesc Campoy Flores

Other Decks in Programming

Transcript

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

    collected - simple - fast - cool - awesome - …
  2. Scale axis Number of: cores concurrency machines networking lines of

    code compilation speed / maintainability developers readability
  3. 1. The language a. the syntax b. the type system

    c. the concurrency support 2. The tooling 3. The community The cool parts
  4. Hello, world package main import "fmt" func main() { msg

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

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

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

    Øredev" fmt.Println(msg) } Hello, world
  8. func fib() func() int { a, b := 0, 1

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

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

    0, 1 return func() int { a, b = b, a+b return a } }
  11. Describe memory layout Every value is of a concrete type

    Concrete types can be: - simple (predefined) or composed - named or anonymous Concrete types
  12. var ( num int // an integer tenNums [10]int //

    an array of ten integers ) Some concrete types
  13. 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
  14. 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
  15. 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
  16. // a function that given a float returns a float

    var mathFunc func(float64) float64 Some concrete types: anonymous types
  17. // 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
  18. Definition of named types: type XXX def Type conversion between

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

    float64 c := Celsius(100) f := Fahrenheit(100) fmt.Println(c + f)
  20. 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)
  21. 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
  22. Methods type Person struct { Name string Age int }

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

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

    any concrete type can satisfy an interface Interfaces
  25. Very lightweight threads: - 2KB of memory footprint - handled

    by the Go runtime (fast switching) Goroutines
  26. 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
  27. 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
  28. 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
  29. Performance Generating 16Mpx: - sequentially - with wait groups -

    with channels github.com/campoy/mandelbrot
  30. 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
  31. Generate binaries ready to deploy with scp … Static linking

    + cross compilation … for any platform!
  32. Come tomorrow for more on tooling! - code formatting -

    code analysis - debugging - etc. ANALYZING GO PROGRAMS: NO READING REQUIRED And many more tools
  33. Proposing Changes to Go An effort to make the community:

    - open (github.com/golang/proposal) - welcoming - transparent
  34. 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
  35. Conferences: - dotGo - Paris (next week!) - GopherCon -

    Denver, Dubai, and India - GoCon - Tokyo - GolangUK - many more Lively community