Introduction to Go

D1b1ae7d58da3d0cb7db07e308758e9e?s=47 The Guild
September 05, 2013

Introduction to Go

Pascal Widdershoven gives The Guild (http://theguild.nl) an introduction to and possible uses of Go, a new programming language by Google. Video available at https://vimeo.com/74244545

D1b1ae7d58da3d0cb7db07e308758e9e?s=128

The Guild

September 05, 2013
Tweet

Transcript

  1. By Pascal Widdershoven Introduction to Go

  2. Who am I? Pascal Widdershoven Developer @ Kabisa

  3. • Young, open source, modern programming language inspired by C

    • Developed internally at Google, beginning in 2007 • Officially announced in November 2009 • Now at 1.1 What is Go?
  4. • Statically typed, garbage collected • Concurrency as a first

    class citizen • Rich standard library, “batteries included” • Fast compilation, cross platform Key features
  5. package main import "fmt" // fib returns a function that

    returns // successive Fibonacci numbers. func fib() func() int { a, b := 0, 1 return func() int { a, b = b, a+b return a } } func main() { f := fib() // Function calls are evaluated left-to-right. fmt.Println(f(), f(), f(), f(), f()) } Closure Type inference Look ma, no semicolons!
  6. • Goroutines can be thought of as cheap threads •

    Goroutines communicate through channels Goroutines
  7. func main() { c1 := produce("Chan 1") c2 := produce("Chan

    2") for i := 0 ; i < 10 ; i++ { select { case msg1 := <- c1: fmt.Println(msg1) case msg2 := <- c2: fmt.Println(msg2) } } } func produce(cName string) <-chan string { c := make(chan string) go func() { for i := 0; ; i++ { c <- fmt.Sprintf("Ping %d on %s", i, cName) time.Sleep(time.Duration(rand.Intn(3)) * time.Second) } }() return c } Producer Consumer Create producers Goroutine
  8. Objected oriented? • Yes and no • No type hierarchy

    • Go structs are a bit like classes • Functions can be defined on structs
  9. type Person struct { Firstname string Lastname string Age int

    } func(p *Person) Fullname() string { return fmt.Sprintf("%s %s", p.Firstname, p.Lastname) } func main() { p := Person{"Pascal", "Widdershoven", 24} fmt.Println(p.Fullname()) // => Pascal Widdershoven } Struct definition Method receiver Call method on struct
  10. Error handling • No exceptions! • Functions return error objects

    dir, err := os.Open(".") if err != nil { return } • Multiple return values are used extensively
  11. The ugly • Features not implemented (yet) • Package management

    • Immature eco-system • Lack of tooling
  12. Go ahead • http://tour.golang.org/ • http://play.golang.org/ • http://golang.org/doc/code.html • http://golang.org/doc/effective_go.html

    • http://www.golang-book.com/ • https://gobyexample.com/
  13. Questions ?