go, the one language to learn in 2014

71
golang the one language you have to try in 2014

Upload: andrzej-grzesik

Post on 27-Jun-2015

471 views

Category:

Technology


0 download

DESCRIPTION

Some features I like in go

TRANSCRIPT

Page 1: Go, the one language to learn in 2014

golangthe one language you have to try in 2014

Page 2: Go, the one language to learn in 2014

golangthe one language you have to try in 2014

Page 3: Go, the one language to learn in 2014

Andrzej Grzesik!

!

!

@ags313

[email protected]

andrzejgrzesik.info

Page 4: Go, the one language to learn in 2014

about:me

Page 5: Go, the one language to learn in 2014

dev going deeper

Page 6: Go, the one language to learn in 2014

disclaimers

Page 7: Go, the one language to learn in 2014

my opinions are my own

Page 8: Go, the one language to learn in 2014

I hate computers

Page 9: Go, the one language to learn in 2014

questions?shoot!

Page 10: Go, the one language to learn in 2014

golang

Page 11: Go, the one language to learn in 2014

gopher

Page 12: Go, the one language to learn in 2014

free and open source

Page 13: Go, the one language to learn in 2014

BSD licensed

Page 14: Go, the one language to learn in 2014

comes from G

Page 15: Go, the one language to learn in 2014

FASTand I mean FAST

Page 16: Go, the one language to learn in 2014

tl;dr;C++ and ruby had a wild time

Page 17: Go, the one language to learn in 2014

play with it tonight

Page 18: Go, the one language to learn in 2014

so, why do I like go?

Page 19: Go, the one language to learn in 2014

no runtime dependencies!

Page 20: Go, the one language to learn in 2014

more pleasant than C

Page 21: Go, the one language to learn in 2014

go toolchain

Page 22: Go, the one language to learn in 2014

go command

Page 23: Go, the one language to learn in 2014

most important thing

Page 24: Go, the one language to learn in 2014

there is only one formatting

Page 25: Go, the one language to learn in 2014

package main!!

import "fmt"!!

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

Page 26: Go, the one language to learn in 2014

types

Page 27: Go, the one language to learn in 2014

types• uint8, uint16, uint32, uint64

• int8, int16, int32, int64

• float32, float64

• complex64, complex128

• byte alias for uint8

• rune alias for int32

• string

Page 28: Go, the one language to learn in 2014

func program() {! var text! text = “zomg"! more := "zomg"!!

fmt.Println(len(text));!}!

Page 29: Go, the one language to learn in 2014

maps

Page 30: Go, the one language to learn in 2014

func main() {! attendees := map[string]bool{! "Phil": true,! "Marcin": true,! }!!

fmt.Println(attendees["Phil"]) // true! fmt.Println(attendees["ags"]) // false! partygoers["ags"] = true! fmt.Println(attendees["ags"]) // true!}!

Page 31: Go, the one language to learn in 2014

structs

Page 32: Go, the one language to learn in 2014

type Rectangle struct {! a, b int32!}!!

func main() {! var rect Rectangle! rect = Rectangle{5, 10}! rect = Rectangle{a: 10, b: 5}!!

HasArea(s).Area()!}

Page 33: Go, the one language to learn in 2014

type Square struct {! side int32!}!!

func (sq Square) Area() int32 {! return sq.side * sq.side!}!!

func main() {! s := Square{16}! area := s.Area()!}

Page 34: Go, the one language to learn in 2014

interfaces

Page 35: Go, the one language to learn in 2014

type Square struct {! side int32!}!!

func (sq Square) Area() int32 {! return sq.side * sq.side!}!!

type HasArea interface {! Area() int32!}!!

func main() {! s := Square{16}! HasArea(s).Area()!}

Page 36: Go, the one language to learn in 2014

goroutineslightweight threads

Page 37: Go, the one language to learn in 2014

func f(i int) {! amt := rand.Intn(1000)! time.Sleep(time.Duration(amt) * time.Millisecond)! fmt.Println(i)!}!!

func main() {! for i := 0; i < 3; i++ {! go f(i)! }! var input string! fmt.Scanln(&input)!}

Page 38: Go, the one language to learn in 2014

how many will run? runtime.GOMAXPROCS(4)

Page 39: Go, the one language to learn in 2014

channels

Page 40: Go, the one language to learn in 2014

channels

• communicate between funcs

• typed

• thread-safe

Page 41: Go, the one language to learn in 2014

channelschannel := make(chan int)!

Page 42: Go, the one language to learn in 2014

unbuffered channels

• sync

• will wait when empty

Page 43: Go, the one language to learn in 2014

buffered channelschannel := make(chan int, size)!

Page 44: Go, the one language to learn in 2014

buffered channels

• async

• return 0 element when empty

• will only wait when full

Page 45: Go, the one language to learn in 2014

basicschannel := make(chan int)!c <- a!!

<- c!!

a = <- c!!

a, ok = <- c!

Page 46: Go, the one language to learn in 2014

func program() {! channel := make(chan int) !}!!

func from(connection chan int) {! connection <- rand.Intn(255)!}!!

func to(connection chan int) {! i := <- connection! fmt.Println(“much received", i)!}!

Page 47: Go, the one language to learn in 2014

but that’s not cool yet

Page 48: Go, the one language to learn in 2014

coordinate routines

Page 49: Go, the one language to learn in 2014

func program() {! channel := make(chan int) !!

go func() {! close(channel)! // or! channel <- anything! }()!!

<- channel!}!

Page 50: Go, the one language to learn in 2014

func program() {! latch := make(chan int) !!

go worker()! close(latch)!}!!

func worker() {! <- latch !}!

Page 51: Go, the one language to learn in 2014

generators

Page 52: Go, the one language to learn in 2014

id := make(chan int64)! go func() {! var counter int64 = 0! for {! id <- counter! counter += 1! } !}()

Page 53: Go, the one language to learn in 2014

multiple channels at once!

Page 54: Go, the one language to learn in 2014

func program() {! select {! case a := <- channel!!

case b, mkay := other!!

case output <- z!!

default:! }!}!

Page 55: Go, the one language to learn in 2014

ranges

Page 56: Go, the one language to learn in 2014

func fillIn(channel chan int) {! channel <- 1! channel <- 2! channel <- 4! close(channel)!}!!

func main() {! channel := make(chan int)! go fillIn(channel)!!

for s := range channel {! fmt.Printf("%d \n", s)! }!}

Page 57: Go, the one language to learn in 2014

packages

Page 58: Go, the one language to learn in 2014

[18:48][agrzesik@melmac:~/vcs/talks/go/hello]!$ find .!.!./bin!./bin/main!./pkg!./pkg/darwin_amd64!./pkg/darwin_amd64/hello.a!./src!./src/hello!./src/hello/hello.go!./src/main!./src/main/.main.go.swp!./src/main/main.go!

Page 59: Go, the one language to learn in 2014

import (! "code.google.com/p/go.net/websocket"! "fmt"! "net/http"!)!

Page 60: Go, the one language to learn in 2014

go get

Page 61: Go, the one language to learn in 2014

net

Page 62: Go, the one language to learn in 2014

echo server

Page 63: Go, the one language to learn in 2014

const listenAddr = "localhost:4000"!!

func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! io.Copy(c, c)! }!}

Page 64: Go, the one language to learn in 2014

concurrent echo server

Page 65: Go, the one language to learn in 2014

const listenAddr = "localhost:4000"!!

func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! go io.Copy(c, c)! }!}

Page 66: Go, the one language to learn in 2014

const listenAddr = "localhost:4000"!!

func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! io.Copy(c, c)! }!}

Page 67: Go, the one language to learn in 2014

websockets?

Page 68: Go, the one language to learn in 2014

func main() {! http.Handle("/", websocket.Handler(handler))! http.ListenAndServe("localhost:1984", nil)!}!!

func handler(c *websocket.Conn) {! var s string! fmt.Fscan(c, &s)! fmt.Println("Received:", s)! fmt.Fprint(c, “hey!”)!}!

Page 69: Go, the one language to learn in 2014

so, what looks bad?

Page 70: Go, the one language to learn in 2014

type AssetMetas struct {!!Metas []AssetMeta `json:"assetMetas"`! }!!

type AssetMeta struct {!!ResourceName string `json:"resource_name"`! !Md5 string `json:"md5"`! !Urls []string `json:"urls"`! }!

Page 71: Go, the one language to learn in 2014

so, go code!