Slide 1

Slide 1 text

Go: a language built for getting things done Sarah Wells Principal Engineer, Financial Times @sarahjwells

Slide 2

Slide 2 text

@sarahjwells Hello

Slide 3

Slide 3 text

@sarahjwells Building a microservices architecture

Slide 4

Slide 4 text

@sarahjwells Running applications in containers

Slide 5

Slide 5 text

@sarahjwells The JVM makes docker images bigger

Slide 6

Slide 6 text

@sarahjwells Image size container memory used vs

Slide 7

Slide 7 text

@sarahjwells Enthusiastic gopher on the team!

Slide 8

Slide 8 text

@sarahjwells About Go Robert Griesemer, Rob Pike, and Ken Thompson

Slide 9

Slide 9 text

@sarahjwells “Efficient, scalable and productive”

Slide 10

Slide 10 text

@sarahjwells • Started Sept 2007 • Announced and open sourced in Nov 2009 • Go 1.0 released March 2012 • Now on version 1.7 (released 2016/08/15) • Both 1.6 and 1.7 releases were mostly changes in the implementation of the toolchain, runtime, and libraries

Slide 11

Slide 11 text

@sarahjwells Built to solve Google’s problems

Slide 12

Slide 12 text

@sarahjwells – Rob Pike keynote at SPLASH 2012 conference “to eliminate the slowness and clumsiness of software development at Google, and thereby to make the process more productive and scalable”

Slide 13

Slide 13 text

@sarahjwells Can’t be too radical, need developers to be productive quickly

Slide 14

Slide 14 text

@sarahjwells Must cope with the modern world: multicore machines, web applications

Slide 15

Slide 15 text

@sarahjwells Must work at scale: large programmes, lots of developers working on them

Slide 16

Slide 16 text

@sarahjwells Designed to make tools easy to write

Slide 17

Slide 17 text

@sarahjwells Getting started with Go

Slide 18

Slide 18 text

@sarahjwells • https://golang.org/ • Install Go • Create a workspace directory, e.g. go-workspace • set $GOPATH to point to that workspace

Slide 19

Slide 19 text

@sarahjwells package main import ( "fmt" "github.com/golang/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello world")) }

Slide 20

Slide 20 text

@sarahjwells package main import ( "fmt" "github.com/golang/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello world")) }

Slide 21

Slide 21 text

@sarahjwells package main import ( "fmt" "github.com/golang/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello world")) }

Slide 22

Slide 22 text

@sarahjwells package main import ( "fmt" "github.com/golang/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello world")) }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

@sarahjwells package main import ( "fmt" "github.com/golang/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello world")) }

Slide 25

Slide 25 text

@sarahjwells Go tooling

Slide 26

Slide 26 text

@sarahjwells go fmt - formats your code according to specific rules • Easier to write • Easier to read • Easier to maintain • Fewer arguments!

Slide 27

Slide 27 text

@sarahjwells Allows you to easily do automatic code transformations

Slide 28

Slide 28 text

@sarahjwells FT-MW4708:wosr sarah.wells$ go help Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files doc show documentation for package or symbol env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources

Slide 29

Slide 29 text

@sarahjwells … generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages

Slide 30

Slide 30 text

@sarahjwells Go workspace

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

go get github.com/golang/example/hello

Slide 33

Slide 33 text

@sarahjwells Let’s have a look at some more code

Slide 34

Slide 34 text

@sarahjwells Functions

Slide 35

Slide 35 text

@sarahjwells func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return } func main() { x, y := split(15) fmt.Printf("x=%d, y=%d\n", x, y) //outputs x=6, y=9 }

Slide 36

Slide 36 text

@sarahjwells func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return } func main() { x, y := split(15) fmt.Printf("x=%d, y=%d\n", x, y) //outputs x=6, y=9 }

Slide 37

Slide 37 text

@sarahjwells Error handling

Slide 38

Slide 38 text

@sarahjwells type error interface { Error() string }

Slide 39

Slide 39 text

@sarahjwells package os func Open(name string) (file *File, err error) … f, err := os.Open("filename.ext") if err != nil { log.Fatal(err) } // do something with the open *File f

Slide 40

Slide 40 text

@sarahjwells Objects

Slide 41

Slide 41 text

@sarahjwells type Rect struct { width int height int } func (r Rect) Area() int { return r.width * r.height } func main() { r := Rect{width: 10, height: 5} fmt.Printf("area: %d\n", r.area()) }

Slide 42

Slide 42 text

@sarahjwells type Rectangle struct { width int height int } func (r Rectangle) Area() int { return r.width * r.height } func main() { r := Rectangle{width: 10, height: 5} fmt.Printf("area: %d\n", r.Area()) }

Slide 43

Slide 43 text

@sarahjwells Interfaces

Slide 44

Slide 44 text

@sarahjwells type Writer interface { Write(p []byte) (n int, err error) }

Slide 45

Slide 45 text

@sarahjwells package os func (f *File) Write(b []byte) (n int, err error) { if f == nil { return 0, ErrInvalid } … }

Slide 46

Slide 46 text

@sarahjwells type ByteCounter struct { counter int } func (c *ByteCounter) Write(p []byte) (int, error) { (*c).counter += len(p) return len(p), nil }

Slide 47

Slide 47 text

@sarahjwells package fmt // writes to the supplied writer func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { p := newPrinter() p.doPrintf(format, a) n, err = w.Write(p.buf) p.free() return }

Slide 48

Slide 48 text

@sarahjwells Concurrency

Slide 49

Slide 49 text

@sarahjwells func main() { fmt.Printf("Hello\n") go slowFunction() fmt.Printf("Bye!\n") } func slowFunction() { //do something slow time.Sleep(1 * time.Second) fmt.Printf("Finished slow stuff\n") }

Slide 50

Slide 50 text

@sarahjwells ch := make(chan int) ch <- v // Send v to channel ch. v := <-ch // Receive from ch, and // assign value to v.

Slide 51

Slide 51 text

@sarahjwells func main() { rand.Seed(time.Now().UnixNano()) responses := make(chan string, 5) for i := 1; i <= 5; i++ { go slowFunction(i, responses) } fmt.Printf("Winner was %s\n", <-responses) } func slowFunction(num int, out chan<- string) { waitSeconds := rand.Intn(10) fmt.Printf("Num %d is waiting %d seconds\n", num, waitSeconds) time.Sleep(time.Duration(waitSeconds) * time.Second) out <- fmt.Sprintf("%d", num) }

Slide 52

Slide 52 text

@sarahjwells func main() { rand.Seed(time.Now().UnixNano()) responses := make(chan string, 5) for i := 1; i <= 5; i++ { go slowFunction(i, responses) } fmt.Printf("Winner was %s\n", <-responses) } func slowFunction(num int, out chan<- string) { waitSeconds := rand.Intn(10) fmt.Printf("Num %d is waiting %d seconds\n", num, waitSeconds) time.Sleep(time.Duration(waitSeconds) * time.Second) out <- fmt.Sprintf("%d", num) }

Slide 53

Slide 53 text

@sarahjwells func main() { rand.Seed(time.Now().UnixNano()) responses := make(chan string, 5) for i := 1; i <= 5; i++ { go slowFunction(i, responses) } fmt.Printf("Winner was %s\n", <-responses) } func slowFunction(num int, out chan<- string) { waitSeconds := rand.Intn(10) fmt.Printf("Num %d is waiting %d seconds\n", num, waitSeconds) time.Sleep(time.Duration(waitSeconds) * time.Second) out <- fmt.Sprintf("%d", num) }

Slide 54

Slide 54 text

@sarahjwells That webservice…

Slide 55

Slide 55 text

@sarahjwells func main() { ah := authorHandler{newHardCodedAuthorService()} r := mux.NewRouter() r.HandleFunc("/authors/{uuid}", ah.getAuthor).Methods("GET") http.Handle("/", r) log.Fatal(http.ListenAndServe("localhost:8000", nil)) } type authorHandler struct { as authorService }

Slide 56

Slide 56 text

@sarahjwells func main() { ah := authorHandler{newHardCodedAuthorService()} r := mux.NewRouter() r.HandleFunc("/authors/{uuid}", ah.getAuthor).Methods("GET") http.Handle("/", r) log.Fatal(http.ListenAndServe("localhost:8000", nil)) } type authorHandler struct { as authorService }

Slide 57

Slide 57 text

@sarahjwells func (ah *authorHandler) getAuthor(writer http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) uuid := vars["uuid"] requestedAuthor, found := ah.as.getAuthorByUUID(uuid) if !found { writer.WriteHeader(http.StatusNotFound) return } enc := json.NewEncoder(writer) //should have error handling here enc.Encode(requestedAuthor) }

Slide 58

Slide 58 text

@sarahjwells type authorService interface { getAuthorByUUID(uuid string) (author, bool) } type simpleAuthorService struct { authors map[string]author } func (as simpleAuthorService) getAuthorByUUID(uuid string) (author, bool) { requestedAuthor, found := as.authors[uuid] return requestedAuthor, found }

Slide 59

Slide 59 text

@sarahjwells type author struct { Name string `json:"name"` Email string `json:"email"` ImageURL string `json:"imageurl"` Biography string `json:"biography"` TwitterHandle string `json:"twitterhandle"` TmeIdentifier string `json:"tmeidentifier"` }

Slide 60

Slide 60 text

@sarahjwells Summary

Slide 61

Slide 61 text

@sarahjwells We’ve talked about: • the language aims • tooling • some of the interesting design decisions

Slide 62

Slide 62 text

@sarahjwells So who’s using Go? • Docker is written in Go • Dropbox use Go (as well as Python) • Netflix rewrote their chaos monkey in Go

Slide 63

Slide 63 text

@sarahjwells Resources

Slide 64

Slide 64 text

@sarahjwells • https://golang.org - the home of Go, including go docs • Start with the Go tour: https://tour.golang.org/welcome/1 • Then Effective Go: https://golang.org/doc/effective_go.html • Go by Example: https://gobyexample.com/

Slide 65

Slide 65 text

@sarahjwells http://www.gopl.io/ https://github.com/adonovan/ gopl.io

Slide 66

Slide 66 text

@sarahjwells Code samples: https://github.com/sjwells/wosr

Slide 67

Slide 67 text

@sarahjwells Thank you!