Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Go programming language

February 17, 2017

The Go programming language

A fast smooth introduction to the language 2


February 17, 2017

More Decks by yageek

Other Decks in Technology


  1. The Go programming language A fast smooth introduction to the

    language 17 February 2017 Yannick Heinrich EEA Engineer, SQLI Suisse SA
  2. 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. 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/)
  4. Reminder Concurrency !== Parallelism Concurrency: Programming as the composition of

    independently executing processes. Parallelism: Programming as the simultaneous execution of (possibly related) computations
  5. 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
  6. 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/)
  7. 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. 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/
  9. 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
  10. 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)".
  11. 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)
  12. 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)
  13. 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
  14. 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)
  15. 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)
  16. 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
  17. Error management package main import ( "log" "net/http" ) func

    main() { _, err := http.Get("https://www.sqliwithwrongDSN.com/") if err != nil { log.Fatal(err) } } Run
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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/)
  27. 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
  28. 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)