Slide 1

Slide 1 text

#golang

Slide 2

Slide 2 text

Pascal C/Asm Perl Java Evolution of a software developer PaintGo

Slide 3

Slide 3 text

A little bit of history… 2007 2009 2012 2013 2015 The beginning Open sourced 1.0 1.1 1.5

Slide 4

Slide 4 text

Where used? https://github.com/golang/go/wiki/GoUsers Google, Docker, Heroku, Cloud Foundry, CoreOS, InfluxDB, Dropbox, OpenShift, SoundCloud, Toggl, etc. In the clouds!

Slide 5

Slide 5 text

“Can't reach the pedals. no brakes. gaining speed. eyes bulging in horror. technical debt crash eminent. “ — Tim Dysinger, @dysinger

Slide 6

Slide 6 text

Paul Phillips @extempore2 “go is a terrible, terrible language, yet still a major productivity boost. Amdahl's law in another context.” “You often see languages which are fighting the last war. Go is fighting the War of 1812.”

Slide 7

Slide 7 text

“reduce: what you will do with your expectations after you start with Go” “Rust and Scala drown you in complexity. Go drowns you in simplicity.” Paul Phillips @extempore2

Slide 8

Slide 8 text

Tour of Go tour.golang.org

Slide 9

Slide 9 text

package  main   import  "fmt"   func  main()  {     fmt.Println("Hello,  Devclub!")   }  

Slide 10

Slide 10 text

package  main   import  "fmt"   func  main()  {     fmt.Println("Hello,  Devclub!")   }   Java-developers reaction to the capital letter in a function name

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Missing stuff No classes No generics No inheritance No method overriding No exceptions etc

Slide 17

Slide 17 text

No generics? No exceptions? Wut!?

Slide 18

Slide 18 text

Features Functions Structures Interfaces Methods Slices Pointers Go-routines Channels

Slide 19

Slide 19 text

func  main()  {     fmt.Println("1  +  2  =",  add(1,  2))   }   func  add(a  int,  b  int)  int  {     return  a  +  b   } Functions

Slide 20

Slide 20 text

func  main()  {     fmt.Println("1  +  2  =",  add(1,  2))   }   func  add(a  int,  b  int)  int  {     return  a  +  b   } Functions

Slide 21

Slide 21 text

func  main()  {     fmt.Println("1  +  2  =",  add(1,  2))   }   func  add(a,  b  int)  int  {     return  a  +  b   } Functions

Slide 22

Slide 22 text

func  main()  {      s,  p  :=  calculate(1,  2)     fmt.Printf("1+2=%d,  1*2=%d",  s,  p)   }   func  calculate(a,  b  int)  (int,  int)  {      return  a  +  b,  a  *  b   } Functions

Slide 23

Slide 23 text

func  main()  {      s,  p  :=  calculate(1,  2)     fmt.Printf("1+2=%d,  1*2=%d",  s,  p)   }   func  calculate(a,  b  int)  (int,  int)  {      return  a  +  b,  a  *  b   } Functions

Slide 24

Slide 24 text

func  main()  {      s,  p  :=  calculate(1,  2)     fmt.Printf("1+2=%d,  1*2=%d",  s,  p)   }   func  calculate(a,  b  int)  (s,  p  int)  {      s  :=  a  +  b      p  :=  a  *  b      return   } Functions

Slide 25

Slide 25 text

func  main()  {      double  :=  func(a  int)  int  {            return  a  *  2      }     double(14)   }   Functions as values

Slide 26

Slide 26 text

func  main()  {      double  :=  factory()     double(14)   }   func  factory()  func(a  int)  int  {      return  func(a  int)  int  {            return  a  *  2      }   } Functions as values

Slide 27

Slide 27 text

Arrays var  a  [4]int   a[0]  =  1   i  :=  a[0] http://blog.golang.org/go-slices-usage-and-internals arr  :=  [2]string{"Foo",  "Bar"} arr  :=  […]string{"Foo",  "Bar"}

Slide 28

Slide 28 text

Slices a  :=  []int{1,  2,  3}      //  [1  2  3] No size definition b  :=  append(a,  4,  5)    //  [1  2  3  4  5] c  :=  make(c,  3)              //  [0  0  0] d  :=  b[1:3]                      //  [2  3] e  :=  b[:3]                        //  [1  2  3]

Slide 29

Slide 29 text

Maps   monthdays  :=  map[string]int{       "Jan":  31,  "Feb":  28,  "Mar":  31,       "Apr":  30,  "May":  31,  "Jun":  30,       "Jul":  31,  "Aug":  31,  "Sep":  30,       "Oct":  31,  "Nov":  30,  "Dec":  31,     }     for  month,  days  :=  range  monthdays  {       fmt.Println(month,  days)     }

Slide 30

Slide 30 text

Maps   monthdays  :=  map[string]int{       "Jan":  31,  "Feb":  28,  "Mar":  31,       "Apr":  30,  "May":  31,  "Jun":  30,       "Jul":  31,  "Aug":  31,  "Sep":  30,       "Oct":  31,  "Nov":  30,  "Dec":  31,     }     for  month,  days  :=  range  monthdays  {       fmt.Println(month,  days)     } Obligatory comma!?

Slide 31

Slide 31 text

Maps   monthdays  :=  map[string]int{       "Jan":  31,  "Feb":  28,  "Mar":  31,       "Apr":  30,  "May":  31,  "Jun":  30,       "Jul":  31,  "Aug":  31,  "Sep":  30,       "Oct":  31,  "Nov":  30,  "Dec":  31,     }     value,  ok  :=  monthdays["Jan"]
    fmt.Println(value)    //    31      fmt.Println(ok)          //    true

Slide 32

Slide 32 text

Pointers var  p  *int   i  :=  42   p  =  &i     fmt.Println(*p)   *p  =  21   fmt.Println(i) 42 21

Slide 33

Slide 33 text

Pointers var  p  *int   i  :=  42   p  =  &i     fmt.Println(*p)   *p  =  21   fmt.Println(i)

Slide 34

Slide 34 text

Pointers var  p  *int   i  :=  42   p  =  &i     fmt.Println(*p)   *p  =  21   fmt.Println(i) No pointer arithmetics

Slide 35

Slide 35 text

Structures type  Vertex  struct  {      X  int      Y  int   }  

Slide 36

Slide 36 text

Structures type  Vertex  struct  {      X  int      Y  int   }   func  main()  {      fmt.Println(Vertex{1,  2})   }

Slide 37

Slide 37 text

Methods type  Vertex  struct  {      X  int      Y  int   }   func  (v  Vertex)  Total()  int  {      return  v.X  +  v.Y   }

Slide 38

Slide 38 text

Methods type  Vertex  struct  {      X  int      Y  int   }   func  (v  Vertex)  Total()  int  {      return  v.X  +  v.Y   } v  :=  Vertex{1,  2}   v.Total()

Slide 39

Slide 39 text

Interfaces type  Entity  interface  {      Total()  int   }  

Slide 40

Slide 40 text

Interfaces type  Entity  interface  {      Total()  int   }   Vertex implements Entity func  (v  Vertex)  Total()  int  {      return  v.X  +  v.Y   }

Slide 41

Slide 41 text

Interfaces v  :=  Vertex  {1,  2}   blah(v) func  blah(e  Entity)  {        e.Total()   }

Slide 42

Slide 42 text

Interfaces func  foo(x  interface{})  int  {        x.(Vertex).Total()   }

Slide 43

Slide 43 text

Interfaces func  foo(x  interface{})  int  {        x.(Vertex).Total()   } panic:  interface  conversion:  interface  is  main.Vertex2,  not  main.Vertex   goroutine  1  [running]:   main.main()     main.go:25  +0xcd   exit  status  2   foo(Vertex2{1,2})

Slide 44

Slide 44 text

A programmer had a problem. He thought to solve it with threads. two Now problems. he has

Slide 45

Slide 45 text

Go-routines foo(Vertex2{1,2}) go  foo(Vertex2{1,2}) Usual function call Function call in a go-routine

Slide 46

Slide 46 text

func  ready(w  string,  sec  int)  {     time.Sleep(time.Duration(sec)  *  time.Second)     fmt.Println(w,  "is  ready!")   }   func  main()  {     go  ready("Tea",  2)     go  ready("Coffee",  1)     fmt.Println("I'm  waiting")     time.Sleep(5  *  time.Second)   } I’m waiting Coffee is ready Tea is ready

Slide 47

Slide 47 text

Channels ci  :=  make(chan  int)   cs  :=  make(chan  string)   cf  :=  make(chan  interface{}) ci <- 1 ← send value 1 into channel ci i := <-ci ← read int from channel ci

Slide 48

Slide 48 text

var  c  chan  int   func  ready(w  string,  sec  int)  {     time.Sleep(time.Duration(sec)  *  time.Second)     fmt.Println(w,  "is  ready!”)      с  <-­‐  1   }   func  main()  {     go  ready("Tea",  2)     go  ready("Coffee",  1)     fmt.Println("I'm  waiting")      <-­‐c      <-­‐c   }

Slide 49

Slide 49 text

var  done  =  make(chan  bool)   var  msgs  =  make(chan  int)   func  produce()  {     for  i  :=  0;  i  <  10;  i++  {       msgs  <-­‐  i     }     done  <-­‐  true   }   func  consume()  {     for  {       msg  :=  <-­‐msgs       println(msg)     }   }   func  main()  {     go  produce()     go  consume()     <-­‐  done   }

Slide 50

Slide 50 text

var  done  =  make(chan  bool)   var  msgs  =  make(chan  int)   func  produce()  {     for  i  :=  0;  i  <  10;  i++  {       msgs  <-­‐  i     }     done  <-­‐  true   }   func  consume()  {     for  {       msg  :=  <-­‐msgs       println(msg)     }   }   func  main()  {     go  produce()     go  consume()     <-­‐  done   } Start  go-­‐routines

Slide 51

Slide 51 text

var  done  =  make(chan  bool)   var  msgs  =  make(chan  int)   func  produce()  {     for  i  :=  0;  i  <  10;  i++  {       msgs  <-­‐  i     }     done  <-­‐  true   }   func  consume()  {     for  {       msg  :=  <-­‐msgs       println(msg)     }   }   func  main()  {     go  produce()     go  consume()     <-­‐  done   } Send  values

Slide 52

Slide 52 text

var  done  =  make(chan  bool)   var  msgs  =  make(chan  int)   func  produce()  {     for  i  :=  0;  i  <  10;  i++  {       msgs  <-­‐  i     }     done  <-­‐  true   }   func  consume()  {     for  {       msg  :=  <-­‐msgs       println(msg)     }   }   func  main()  {     go  produce()     go  consume()     <-­‐  done   } Receive  values  and  print

Slide 53

Slide 53 text

var  done  =  make(chan  bool)   var  msgs  =  make(chan  int)   func  produce()  {     for  i  :=  0;  i  <  10;  i++  {       msgs  <-­‐  i     }     done  <-­‐  true   }   func  consume()  {     for  {       msg  :=  <-­‐msgs       println(msg)     }   }   func  main()  {     go  produce()     go  consume()     <-­‐  done   } Done!

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Environment https://golang.org/doc/code.html#Workspaces

Slide 56

Slide 56 text

Environment

Slide 57

Slide 57 text

Environment

Slide 58

Slide 58 text

Tooling • go (version, build, test, get, install, …) • gofmt • godoc • Editors (vim, atom, IntelliJ IDEA, Sublime Text)

Slide 59

Slide 59 text

My choice

Slide 60

Slide 60 text

What’s missing? Debugger IntelliJ IDEA plugin is still in development Can use GDB, but better do something else instead Dependency management A lot of different solutions, no de facto standard -vendor option in 1.5

Slide 61

Slide 61 text

Cross-compilation http://dave.cheney.net/2015/03/03/cross-compilation-just-got- a-whole-lot-better-in-go-1-5 env GOOS=linux GOARCH=386 go build hello.go 1. Build on Mac for Linux 2. Run the binary without installing extra dependencies or runtime on Linux host

Slide 62

Slide 62 text

Testing package  math   func  Add(a,  b  int)  int  {     return  a  +  b   } package  math   import  "testing"   func  TestAdd(t  *testing.T){     if  Add(1,  3)  !=  4  {       t.Error("Expecting  4")     }   }  

Slide 63

Slide 63 text

Testing package  math   func  Add(a,  b  int)  int  {     return  a  +  b   } package  math   import  "testing"   func  TestAdd(t  *testing.T){     if  Add(1,  3)  !=  4  {       t.Error("Expecting  4")     }   }   /usr/local/go/bin/go  test  -­‐v  ./...  -­‐run  ^TestAdd$   Testing  started  at  03:35  ...   ?         _/Users/anton/work-­‐src/mygo   [no  test  files]PASS   ok       _/Users/anton/work-­‐src/mygo/math   0.007s

Slide 64

Slide 64 text

Q: Can you build the enterprise apps in Go? And now a million $$ question https://www.youtube.com/watch?v=cFJkLfujOts Building bank in Go: A: Apparently, you can :)

Slide 65

Slide 65 text

Can I haz decimal type? https://github.com/shopspring/decimal import  "github.com/shopspring/decimal"   …   x,  _  :=  decimal.NewFromString("0.1")
 sum  :=  x.Add(x).Add(x) // 0.3

Slide 66

Slide 66 text

import  (     "net/http"     "fmt"   )   func  handler(w  http.ResponseWriter,                              r  *http.Request)  {     fmt.Fprintf(w,  "Welcome!")   }   func  main()  {     http.HandleFunc("/",  handler)     http.ListenAndServe(":8080",  nil)   } Compare  to  Java?

Slide 67

Slide 67 text

Frameworks Gorilla web toolkit http://www.gorillatoolkit.org/ Go-Kit http://gokit.io List of various frameworks and libraries: https://github.com/avelino/awesome-go

Slide 68

Slide 68 text

Resources http://www.miek.nl/downloads/Go/Learning-Go-latest.pdf https://tour.golang.org http://blog.golang.org https://www.youtube.com/playlist? list=PLMW8Xq7bXrG58Qk-9QSy2HRh2WVeIrs7e https://www.youtube.com/channel/ UCx9QVEApa5BKLw9r8cnOFEA Gopher Academy: dotGo: https://gist.github.com/kachayev/21e7fe149bc5ae0bd878 Channels are not enough: http://dave.cheney.net/2015/08/08/performance- without-the-event-loop Performance without event loop:

Slide 69

Slide 69 text

@antonarhipov http://www.slideshare.net/arhan https://speakerdeck.com/antonarhipov