Slide 1

Slide 1 text

The Go programming language A fast smooth introduction to the language 17 February 2017 Yannick Heinrich EEA Engineer, SQLI Suisse SA

Slide 2

Slide 2 text

Summary Issues related to software development Go at the rescue Go AFK Go from the trenches

Slide 3

Slide 3 text

Issues related to software development

Slide 4

Slide 4 text

Issue #1 - Readability and Con dence You rarely work alone on the code. If yes, your code will stay, but on your concern, we never know.

Slide 5

Slide 5 text

Issue #2 - Deployment/Distribution The longer you take to deploy, the longer you will take to solve a production issue. Accessing libraries registry may need Github mirroring. Targeting several OS can become painful Deployment with Rails at Parse.com 20 minutes to do a full deploy or rollback Unicorn, Capistrano, RVM, lots of gems How We Moved Our API From Ruby to Go and Saved Our Sanity (http://blog.parse.com/learn/how-we-moved-our-api- from-ruby-to-go-and-saved-our-sanity/)

Slide 6

Slide 6 text

Reminder Concurrency !== Parallelism Concurrency: Programming as the composition of independently executing processes. Parallelism: Programming as the simultaneous execution of (possibly related) computations

Slide 7

Slide 7 text

Reminder 1 Process === 1 Application, 1 Thread === 1 Task within an Application 1 Thread ber CPU/Core Thread operations are costly in time Typical relationsip between threads and processes. http://www.javamex.com/tutorials/threads/how_threads_work.shtml

Slide 8

Slide 8 text

Issue #3 - Performance and Concurrency The less server you use, the less it will cost. Dealing with concurrency can become seriously complex. Some interpreted languages does not allow parallelism (CRuby, CPython). Global interpreter lock (https://en.wikipedia.org/wiki/Global_interpreter_lock) Parallelism is a Myth in Ruby (https://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/)

Slide 9

Slide 9 text

Go at the rescue

Slide 10

Slide 10 text

Deal with Issue #1 - Readability and Con dence Syntax is not important... - unless you are a programmer , Rob Pike One syntax to rule them all Duck typing checked at compilation No generics No exceptions but error values No inheritance but composition Pointer but no pointer arithmetics

Slide 11

Slide 11 text

Deal with Issue #2 - Deployment/Distribution No more sword ght No dependency hell, you get one binary, period. Cross-Platform, Cross-Architecture and Cross-compilation just easy https://xkcd.com/303/

Slide 12

Slide 12 text

Deal with Issue #2 - Performance and Concurrency 100% native code No libc, but native C FFI. Optimization with Assembly Synchronisation based on CSP (http://spinroot.com/courses/summer/Papers/hoare_1978.pdf) (like Erlang, Rust, Akka in Scala) Use kind of Green Threads mapped on OS threads called goroutine

Slide 13

Slide 13 text

Iron.IO - How We Went from 30 Servers to 2: Go (http://blog.iron.io/2013/03/how-we-went-from-30-servers-to-2-go.html) Dropped from 30 to 2 servers and the second server was used only for redundancy. CPU utilization dropped to less than 5%. Memory usage dropped. Only a "few hundred KB's of memory (on startup) vs our Rails apps which were ~50MB (on startup)".

Slide 14

Slide 14 text

Go AFK

Slide 15

Slide 15 text

Go history Project started as internal project at Google in 2007 and Open-sourced in 2009 with a BSD-style license Backed by brilliant engineers: Rob Pike, Kenneth Thomson, Robert Griesemer, Brad Fitzpatrick and many others. Developed to be good targeting server development. In fact, good for a lot of other stu . Gopher by Renée French (http://www.reneefrench.com)

Slide 16

Slide 16 text

Come with batteries included SQL Database Syscall TCP/IP, FTP, HTTP1/2, Http Push, SMTP, SSH, TLS JSON, XML, HTML, templating, CSV Unicode Image processing Crypto: RSA, AES, DES, SHA, TLS, x509 Compression: bz2, gz, lzw, zip Math Go AST, debug, sytax Re ection (introspection) Testing framework (assertions)

Slide 17

Slide 17 text

Gopher is the new hipster Interest is growing good The growth of Google searches for the term "golang" over the years Dozen of Go User Groups Big meetings: dotGo, GopherIndia, GothamGo, GopherCon, GoCon

Slide 18

Slide 18 text

Some awesome Go projects Celebrities: - Docker (https://docker.io) : An open platform for distributed applications for developers and sysadmins - consul (https://www.consul.io) : Service Discovery - etcd (https://github.com/coreos/etcd) : Key-value store for shared con guration and service discovery - Terraform (https://www.terraform.io) : Terraform is a tool for building, changing, and combining infrastructure safely and e ciently. Others: - Go Mobile (https://github.com/golang/go/wiki/Mobile) : Java <-> Go, Objective-C <-> Go - limetext (http://limetext.org/) : Lime Text is a powerful and elegant text editor primarily developed in Go that aims to be a Free and open-source software successor to Sublime Text. - nes (https://github.com/fogleman/nes) : A Nintendo Entertainment System (NES) emulator written in Go - gopherJS (https://gopherjs.github.io) : GopherJS - A compiler from Go to JavaScript (Goroutine included)

Slide 19

Slide 19 text

Who uses Go? Internet: - Google, Uber, Yahoo, Youtube, Zalando, Atlassian, Reddit, Net ix, Facebook, Dropbox, Disqus, Tumblr, Twitch, Heroku, Cloudfare, SoundCloud,... Nerds: - Github, Sourcegraph You did not expect them: - BBC, Novartis, Thomson Reuters, The New-York Times Games: - Zynga More Golang User (https://code.google.com/p/go-wiki/wiki/GoUsers)

Slide 20

Slide 20 text

Go from the trenches

Slide 21

Slide 21 text

Variables package main import "fmt" func main() { a := 3.14 b := "some string" var c float32 = 3.14 var d string = "some string" fmt.Println("a:", a) fmt.Println("b:", b) fmt.Println("c:", c) fmt.Println("d:", d) } Run

Slide 22

Slide 22 text

Error management package main import ( "log" "net/http" ) func main() { _, err := http.Get("https://www.sqliwithwrongDSN.com/") if err != nil { log.Fatal(err) } } Run

Slide 23

Slide 23 text

Custom type package main import ( "fmt" ) type Person struct { Name string } func (p Person) changeName(name string) { p.Name = name } func main() { a := Person{Name: "Georges"} b := &Person{Name: "Martin"} a.changeName("Justin") b.changeName("Justin") fmt.Println("A:", a) fmt.Println("B:", b) } Run

Slide 24

Slide 24 text

Composition over inheritance package main import "fmt" type AccessLevel int type User struct { Login string Password string } type Administrator struct { User User Level AccessLevel } const ( Nothing AccessLevel = iota God ) func main() { admin := &Administrator{Level: God, User: User{Login: "su", Password: "3.1419527"}} fmt.Println("Admin:", admin) } Run

Slide 25

Slide 25 text

Interface and Duck typing When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck James Whitcomb Riley Go deals with polymorphism through the concept of interface No need to import the interface, just implement

Slide 26

Slide 26 text

package main import "fmt" type Animal interface { Noise() string } type Dog struct{} func (d *Dog) Noise() string { return "waf" } type Cat struct{} func (c Cat) Noise() string { return "miaou" } func main() { var animalA Animal = &Dog{} var animalB Animal = Cat{} fmt.Println("Animal A:", animalA.Noise()) fmt.Println("Animal B:", animalB.Noise()) } Run

Slide 27

Slide 27 text

Goroutine package main import ( "fmt" "time" ) func work(n time.Duration) { time.Sleep(n * time.Millisecond) fmt.Printf("%d ms\n", n) } func main() { go work(400) go work(200) work(1000) } Run

Slide 28

Slide 28 text

Channel package main import ( "fmt" "time" ) func work(ch chan bool, n time.Duration) { time.Sleep(n * time.Millisecond) fmt.Printf("%d ms\n", n) ch <- true } func main() { ch := make(chan bool) go work(ch, 1000) go work(ch, 600) go work(ch, 300) <-ch <-ch <-ch fmt.Println("Finished!") } Run

Slide 29

Slide 29 text

Channel package main import ( "fmt" ) func intGenerator() chan int { ch := make(chan int) id := 0 go func() { for { ch <- id id++ } }() return ch } func main() { generator := intGenerator() fmt.Println("Int:", <-generator) fmt.Println("Int:", <-generator) fmt.Println("Int:", <-generator) } Run

Slide 30

Slide 30 text

Select package main import ( "fmt" "time" ) func longWork() bool { time.Sleep(3 * time.Millisecond) return true } func main() { timeout := time.After(1 * time.Millisecond) ch := make(chan bool) go func() { ch <- longWork() }() select { case <-ch: fmt.Println("Long work finished....") case <-timeout: fmt.Println("Timeout...") } } Run

Slide 31

Slide 31 text

Conclusion Abuse of Go ! Start the Go Tour: tour.golang.org (http://tour.golang.org) See the Go Codewalks: golang.org/doc/codewalk/ (https://golang.org/doc/codewalk/)

Slide 32

Slide 32 text

References The bible, coran and torah: golang.org (http://golang.org) The Go blog: blog.golang.com (http://blog.golang.com) Main GoDoc server: godoc.org (https://godoc.org) Google: search for golang

Slide 33

Slide 33 text

Thanks The Go team Thanks to @soulou (https://github.com/soulou) from Scalingo (https://scalingo.com)

Slide 34

Slide 34 text

Questions

Slide 35

Slide 35 text

Thank you Yannick Heinrich EEA Engineer, SQLI Suisse SA [email protected] (mailto:[email protected]) http://blog.yageek.net (http://blog.yageek.net) @yageek (http://twitter.com/yageek)

Slide 36

Slide 36 text

No content