Go in 7 minutes

11 June 2013

Thomas Kappler

www.thomaskappler.net

Go

Hello world

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, λ!")
}

Plus some awesomeness!

Concurrency

Lightweight pseudo-threads passing data.

Goroutines & channels.

"Don't communicate by sharing memory, share memory by communicating."
― Rob Pike

Concurrency example

ch := make(chan int)

go func() {
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch)
}()

for num := range ch {
    fmt.Println(num)
}

Concurrency example

ch := make(chan int)

go func() {
    for i := 0; i < 10; i++ {
        time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
        ch <- i
    }
    close(ch)
}()

for {
    select {
    case num := <-ch:
        fmt.Println(num)
    case <-time.After(1 * time.Second):
        fmt.Println("Timeout!")
        break
    }
}

Types, Structs and Composition

Make anything typesafe.

type Lang string

type Foo struct { ... }

Types, Structs and Composition

Make anything typesafe.

type Lang string

type Foo struct { ... }

Put methods on anything.

func (l Lang) Awesome() bool { return l == "Go" }

Types, Structs and Composition

Make anything typesafe.

type Lang string

type Foo struct { ... }

Put methods on anything.

func (l Lang) Awesome() bool { return l == "Go" }

Compose without annoying delegates.

type WebTuesdayTalk struct {
    Lang
    presenter string
}

Program composition: interfaces

Structural typing = "static duck typing".

func (f *File) Read(b []byte) (n int, err...)

implements

type Reader interface {
    Read(p []byte) (n int, err error)
}

-> Implement interfaces without creating a dependency.

Practical

Resources

Thank you

Thomas Kappler

www.thomaskappler.net