Slide 1

Slide 1 text

can go oredi anot? singapore gophers meetup - 21 may 2014 an introduction to programming in go

Slide 2

Slide 2 text

peter jihoon kim @raingrove co-founder & cto, nitrous.io

Slide 3

Slide 3 text

what is go?

Slide 4

Slide 4 text

be an easier and safer alternative to C created by google, designed to go is a programming language

Slide 5

Slide 5 text

built-in support for concurrency no manual memory management (gc) string, slice, map, and function closures methods and interfaces for structs type safety. compiled to machine code

Slide 6

Slide 6 text

demo: hello world

Slide 7

Slide 7 text

package main ! import "fmt" ! func main() { fmt.Println("herro woll!”) }

Slide 8

Slide 8 text

variable declaration

Slide 9

Slide 9 text

var foo string var bar = "bar" baz := "baz" type inference

Slide 10

Slide 10 text

function declaration

Slide 11

Slide 11 text

func herro(name string) string { return "herro " + name } ! ! func Bai(name string) string { return "okthxbai " + name } private to this package exported (public)

Slide 12

Slide 12 text

func herro(name string) (h string) {
 h = "herro " + name return } named result
 parameters

Slide 13

Slide 13 text

herro := func(name string) string { return "herro " + name }

Slide 14

Slide 14 text

func herrobai(name string) (string, string) { herro := "herro " + name bai := "okthxbai " + name return herro, bai } ! h, b := herrobai("fren") _, b := herrobai("fren") multiple return values unused return value

Slide 15

Slide 15 text

slices

Slide 16

Slide 16 text

s := []string{"foo", "bar", "baz"} type of value

Slide 17

Slide 17 text

s := []string{"foo", "bar", "baz"} s[0] s[1] = "bear" ! len(s) "foo" 3

Slide 18

Slide 18 text

s = append(s, "qux", "quux") 
 t := []string{"satu", "dua"} s = append(s, t...) s := []string{"foo", "bar", "baz"} converts slice into
 arguments

Slide 19

Slide 19 text

s := [][]string{{"foo", "bar"},{"satu", "dua"}} []string is optional

Slide 20

Slide 20 text

s := [][]string{ {"foo", "bar"}, {"satu", "dua"}, } required

Slide 21

Slide 21 text

maps

Slide 22

Slide 22 text

m := map[string]int{"foo": 1, "bar":2} key value type of value type of key

Slide 23

Slide 23 text

m := map[string]int{"foo": 1, "bar":2} m["foo"] m["bar"] = 200 m["baz"] = 1337 ! len(m) 2 3

Slide 24

Slide 24 text

range

Slide 25

Slide 25 text

for i, v := range s { fmt.Println(i, v) } ! 0 foo 1 bar 2 baz s := []string{"foo", "bar", "baz"} index value

Slide 26

Slide 26 text

for k, v := range m { fmt.Println(k, v) } ! foo 1 bar 2 m := map[string]int{"foo": 1, "bar":2} key value

Slide 27

Slide 27 text

concurrency

Slide 28

Slide 28 text

go foo() invokes function in a goroutine* * a lightweight thread of execution that can run concurrently

Slide 29

Slide 29 text

sum := func(s []int, c chan int) { sum := 0 for _, v := range s { sum += v } c <- sum } ! a := [][]int{{1, 2, 3}, {4, 5, 6}} c := make(chan int) go sum(a[0], c) go sum(a[1], c) x, y := <-c, <-c fmt.Println(x+y) send result to channel* receive result from channel * a typed pipe through which goroutines can communicate

Slide 30

Slide 30 text

demo: sleep sort

Slide 31

Slide 31 text

package main ! import ( "fmt" "time" ) ! func main() { fmt.Println(sleepSort([]int{5, 3, 7, 10, 6, 1, 4, 2})) } ! func sleepSort(s []int) (sorted []int) { c := make(chan int) sorted = make([]int, len(s)) for _, v := range s { go func(n int) { time.Sleep(time.Duration(n) * time.Second) c <- n }(v) } for i := 0; i < len(s); i++ { sorted[i] = <-c } return }

Slide 32

Slide 32 text

structs

Slide 33

Slide 33 text

type Person struct { Name string Age int } exported

Slide 34

Slide 34 text

p := Person{Name: "Pete", Age: 28} ! q := &Person{Name: "Pete", Age: 28} “address of” type: Person type: *Person (pointer to Person)

Slide 35

Slide 35 text

p := Person{Name: "Pete", Age: 28} q := &Person{Name: "Pete", Age: 28} ! p2 := p ! q2 := q copies p points to *q

Slide 36

Slide 36 text

p := Person{Name: "Pete", Age: 28} q := &Person{Name: "Pete", Age: 28} ! p.Name q.Name p.Age = 18 q.Age = 18 "Pete"

Slide 37

Slide 37 text

methods

Slide 38

Slide 38 text

type Person struct { Name string Age int }
 func (this *Person) Greet() { fmt.Println(this.Name + " says hello!") } p := &Person{Name: "Pete", Age: 28} p.Greet() Pete says hello! method declaration method receiver

Slide 39

Slide 39 text

interfaces

Slide 40

Slide 40 text

type Triangle struct { Base float64 Height float64 } ! func (this *Triangle) Area() float64 { return 0.5 * this.Base * this.Height } ! type Rectangle struct { Width float64 Length float64 } ! func (this *Rectangle) Area() float64 { return this.Width * this.Length }

Slide 41

Slide 41 text

type Shape interface { Area() float64 } ! ! shapes := []Shape{ &Triangle{Base: 10.0, Height: 5.0}, &Rectangle{Width: 5.0, Length: 4.0}, } ! for _, shape := range shapes { fmt.Println(shape.Area()) } interfaces are satisfied
 implicitly

Slide 42

Slide 42 text

check out “a tour of go”: tour.golang.org create a free go box at www.nitrous.io where to go from here? topics not covered today: packages, testing, web development… attend future singapore gophers meetups

Slide 43

Slide 43 text

thanks & we’re hiring! www.nitrous.io/jobs