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

Go

 Go

An introductory talk on #golang for the GDG Edmonton meetup.

A recording of one of my early rehearsals for the talk:
https://vimeo.com/65704694

Nathan Youngman

November 05, 2013
Tweet

More Decks by Nathan Youngman

Other Decks in Programming

Transcript

  1. KEN THOMPSON Unix team at Bell Labs, see “Coders at

    Work”.! Created grep in an evening! Designed UTF-8 on a diner placemat Image courtesy of the Computer History Museum
  2. ROB PIKE First window system for Unix at Bell Labs!

    Co-author of 2 books with Brian Kernighan! Newsqueak, Limbo: Implementations of Tony Hoare’s CSP “Concurrency Is Not Parallelism” - Waza 2012
  3. HELLO GO package main
 
 import (
 "fmt"
 "os"
 )


    
 func main() {
 who := "World"
 
 if len(os.Args) > 1 {
 who = os.Args[1]
 }
 
 fmt.Println("Hello", who)
 }
 $ go build hello.go $ ./hello "GDG Edmonton" Hello GDG Edmonton $ go run hello.go "YEG" Hello YEG
  4. COMPILED & DYNAMIC Compilation Very strict Very fast Typing Static

    typing Type inference Literals Low-level primitives! uint, float64 Hash maps! Variable length arrays Memory Pointers Garbage collected Interfaces Statically checked Implicitly satisfied
  5. HELLO OOP package main
 
 import "fmt"
 
 type Population

    int
 
 func (population Population) Greet() {
 fmt.Printf("Hello all the %d people!\n", population)
 }
 
 func main() {
 world := Population(6973738433)
 world.Greet()
 }

  6. STRUCTURES package main
 
 import "fmt"
 
 type Person struct

    {
 FirstName, LastName string
 }
 
 func (person Person) Greet() {
 fmt.Println("Hello", person.FirstName, person.LastName)
 }
 
 func main() {
 dan := Person{"Daniel", "Huckstep"}
 dan.Greet()
 }

  7. INTERFACES // ...
 type Greeter interface {
 Greet()
 }
 


    func GreetEveryone(everyone ...Greeter) {
 for _, someone := range everyone {
 someone.Greet()
 }
 }
 
 func main() {
 world := Population(6973738433)
 dan := Person{"Daniel", "Huckstep"}
 GreetEveryone(world, dan)
 }
  8. C10K Linux! pthreads 8MB! (default) Windows threads 1MB Ruby !

    fibers 4K Typical stack Stack Heap SP Program Stack 2 Segmented stacks Goroutines 8K+ Memory layout SP
  9. GOROUTINES // ...
 func Postcard(from string) {
 fmt.Println("Hello from", from)


    }
 
 func main() {
 Postcard("Main")
 
 go Postcard("A goroutine")
 
 go func() {
 fmt.Println("A closure")
 }()
 
 time.Sleep(1 * time.Second)
 }
  10. CONCURRENCY func main() {
 channel := make(chan string)
 go fish(channel)


    
 for fish := range channel {
 fmt.Println(fish)
 }
 }
 
 func fish(channel chan<- string) {
 fishes := []string{"Trout", "Salmon", "Perch", "Bass"}
 for _, fish := range fishes {
 channel <- fish
 }
 close(channel)
 }
  11. CONCURRENT COMPOSITION func play(channel chan<- string) {
 channel <- "Plants

    vs. Zombies"
 channel <- "Fruit Ninja"
 }
 
 func main() {
 river, appstore := make(chan string), make(chan string)
 go fish(river); go play(appstore)
 
 select {
 case fish := <-river:
 fmt.Println("caught a", fish)
 case game := <-appstore:
 fmt.Println("passed", game)
 }
 }
  12. WHAT ELSE? gofmt! exported identifiers! the standard library! import "github.com/lib/pq"!

    arbitrary precision constants! iota for enumerations! slices vs. arrays! pointers vs. unsafe! buffered channels! first-class channels! interior pointers! init()! zero-initialization! constructors! embedding (delegation)! interface{}! type assertions/switches! tagging and reflection! defer! panic/recover! multiple return values
  13. “Go will be the server language of the future.” Tobias

    Lütke (founder, Shopify) Real-Time Web Concurrency Cloud Startup time Resilience Deploy a static binary Web Google Scale Dependency management Developer Happiness Clarity and simplicity
  14. “Go has been described by several engineers here as a

    WYSIWYG language. That is, the code does exactly what it says on the page.” Peter Bourgon (SoundCloud)! golang.org The Go Programming Language ! godoc.org GoDoc ! gobyexample.com Go by Example (literate programming) ! nathany.com/good Go Object Oriented Design ! vimeo.com/49718712 Concurrency Is Not Parallelism ! bit.ly/goyeg Go Edmonton on Google+