Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the...

71
golang the one language you have to try in 2014

description

You live and breathe http. Most of the things you do with a computer involve it. How many tiny little http-related utils have you already created? Is it time to stop? By no means, we all do it. I'd just like to encourage you to write the next one in Go. It's a fun little language, which feels like a cross-breed between C (pointers!) and ruby (concise, powerful syntax). But how much? Why, yet another language, but my perl/python/.. does all the things? Well, it does. But go has a few things that make it super sweet, is web-scale and real fun to use! Or at least come and listen how did we get rid of 87% of our codebase in a day :-) Andrzej Grzesik - I like programming. I do it a lot, mostly on the JVM, usually writing fancy backends for big, distributed systems. I also display a particular affection to continuous delivery.. UI, unless quickly hacked, is not my play ;-) I believe that most problems we deal with are people problems, so I mix and match tools with technologies to achieve my goals, make people happy and achieve world peace :-) I believe in software quality, and organize GeeCON, Polish JUG, Krakow Software Craftsmanship, Cracow Hadoop User Group. In my free time, I read paper books and cycle, a lot!

Transcript of Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the...

Page 1: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

golangthe one language you have to try in 2014

Page 2: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

golangthe one language you have to try in 2014

Page 3: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

Andrzej Grzesik!

!

!

@ags313

[email protected]

andrzejgrzesik.info

Page 4: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

about:me

Page 5: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

dev going deeper

Page 6: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

disclaimers

Page 7: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

my opinions are my own

Page 8: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

I hate computers

Page 9: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

questions?shoot!

Page 10: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

golang

Page 11: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

gopher

Page 12: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

free and open source

Page 13: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

BSD licensed

Page 14: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

comes from G

Page 15: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

FASTand I mean FAST

Page 16: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

Page 17: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

play with it tonight

Page 18: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

so, why do I like go?

Page 19: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

no runtime dependencies!

Page 20: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

more pleasant than C

Page 21: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

go toolchain

Page 22: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

go command

Page 23: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

most important thing

Page 24: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

there is only one formatting

Page 25: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

package main!!

import "fmt"!!

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

Page 26: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

types

Page 27: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

types• uint8, uint16, uint32, uint64

• int8, int16, int32, int64

• float32, float64

• complex64, complex128

• byte alias for uint8

• rune alias for int32

• string

Page 28: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

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

Page 29: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

maps

Page 30: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

structs

Page 32: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

interfaces

Page 35: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

goroutineslightweight threads

Page 37: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

how many will run? runtime.GOMAXPROCS(4)

Page 39: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

channels

Page 40: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

channels

• communicate between funcs

• typed

• thread-safe

Page 41: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

channelschannel := make(chan int)!

Page 42: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

unbuffered channels

• sync

• will wait when empty

Page 43: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

Page 44: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

buffered channels

• async

• return 0 element when empty

• will only wait when full

Page 45: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

<- c!!

a = <- c!!

a, ok = <- c!

Page 46: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

but that’s not cool yet

Page 48: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

coordinate routines

Page 49: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

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

<- channel!}!

Page 50: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

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

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

Page 51: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

generators

Page 52: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

Page 53: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

multiple channels at once!

Page 54: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

case b, mkay := other!!

case output <- z!!

default:! }!}!

Page 55: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

ranges

Page 56: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

packages

Page 58: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

[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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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

Page 60: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

go get

Page 61: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

net

Page 62: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

echo server

Page 63: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

concurrent echo server

Page 65: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

websockets?

Page 68: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

so, what looks bad?

Page 70: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

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: Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

so, go code!