The Go programming language

5bfb729f6ac07e17c2326a7335a3f3be?s=47 yageek
February 17, 2017

The Go programming language

A fast smooth introduction to the language 2



February 17, 2017


  1. 1.

    The Go programming language A fast smooth introduction to the

    language 17 February 2017 Yannick Heinrich EEA Engineer, SQLI Suisse SA
  2. 4.

    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.
  3. 5.

    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 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 ( from-ruby-to-go-and-saved-our-sanity/)
  4. 6.

    Reminder Concurrency !== Parallelism Concurrency: Programming as the composition of

    independently executing processes. Parallelism: Programming as the simultaneous execution of (possibly related) computations
  5. 7.

    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.
  6. 8.

    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 ( Parallelism is a Myth in Ruby (
  7. 10.

    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
  8. 11.

    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
  9. 12.

    Deal with Issue #2 - Performance and Concurrency 100% native

    code No libc, but native C FFI. Optimization with Assembly Synchronisation based on CSP ( (like Erlang, Rust, Akka in Scala) Use kind of Green Threads mapped on OS threads called goroutine
  10. 13.

    Iron.IO - How We Went from 30 Servers to 2:

    Go ( 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)".
  11. 14.
  12. 15.

    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 (
  13. 16.

    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)
  14. 17.

    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
  15. 18.

    Some awesome Go projects Celebrities: - Docker ( : An

    open platform for distributed applications for developers and sysadmins - consul ( : Service Discovery - etcd ( : Key-value store for shared con guration and service discovery - Terraform ( : Terraform is a tool for building, changing, and combining infrastructure safely and e ciently. Others: - Go Mobile ( : Java <-> Go, Objective-C <-> Go - limetext ( : 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 ( : A Nintendo Entertainment System (NES) emulator written in Go - gopherJS ( : GopherJS - A compiler from Go to JavaScript (Goroutine included)
  16. 19.

    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 (
  17. 21.

    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
  18. 22.

    Error management package main import ( "log" "net/http" ) func

    main() { _, err := http.Get("") if err != nil { log.Fatal(err) } } Run
  19. 23.

    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
  20. 24.

    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
  21. 25.

    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
  22. 26.

    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
  23. 27.

    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
  24. 28.

    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
  25. 29.

    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
  26. 30.

    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
  27. 31.

    Conclusion Abuse of Go ! Start the Go Tour:

    ( See the Go Codewalks: (
  28. 32.

    References The bible, coran and torah: ( The Go

    blog: ( Main GoDoc server: ( Google: search for golang
  29. 34.
  30. 35.

    Thank you Yannick Heinrich EEA Engineer, SQLI Suisse SA

    ( ( @yageek (
  31. 36.