Go Basics Workshop

Go Basics Workshop

E44317a06f6ff5fec214d3f54f7ba432?s=128

Johannes Pichler

July 06, 2018
Tweet

Transcript

  1. Go Basics Workshop #NarF

  2. Golang 2

  3. Golang • compiled language • fast compilation • based on

    C & Oberon • v1 release 2012 3
  4. Golang • garbage collection • type safety • has pointers

    ! • concurrency with goroutines • object oriented but not class based 4
  5. Get GOing 5

  6. The basics - package declaration package main import "fmt" func

    main() { fmt.Printf("Hello World!") } 6
  7. The basics - imports package main import "fmt" func main()

    { fmt.Printf("Hello World!") } 7
  8. The basics - main function package main import "fmt" func

    main() { fmt.Printf("Hello World!") } 8
  9. Variables var abc string = "a string variable" var number

    int // number = 0 var emptyString string // emptyString = "" short := "a string variable" // var short string = "a string variable" 9
  10. Variables var abc string = "a string variable" var number

    int // number = 0 var emptyString string // emptyString = "" short := "a string variable" // var short string = "a string variable" 10
  11. Variables var abc string = "a string variable" var number

    int // number = 0 var emptyString string // emptyString = "" short := "a string variable" // var short string = "a string variable" 11
  12. Arrays // have a fixed size types := [5]int //

    [0 0 0 0 0] // values can be changed and retrieve types[2] = 3 // [0 0 3 0 0] val := types[2] // 3 // can be initialized on delaration types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5] // can have more dimensions more := [2][3]int 12
  13. Arrays // have a fixed size types := [5]int //

    [0 0 0 0 0] // values can be changed and retrieve types[2] = 3 // [0 0 3 0 0] val := types[2] // 3 // can be initialized on delaration types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5] // can have more dimensions more := [2][3]int 13
  14. Arrays // have a fixed size types := [5]int //

    [0 0 0 0 0] // values can be changed and retrieved types[2] = 3 // [0 0 3 0 0] val := types[2] // 3 // can be initialized on delaration types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5] // can have more dimensions more := [2][3]int 14
  15. Arrays // have a fixed size types := [5]int //

    [0 0 0 0 0] // values can be changed and retrieve types[2] = 3 // [0 0 3 0 0] val := types[2] // 3 // can be initialized on delaration types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5] // can have more dimensions more := [2][3]int 15
  16. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 16
  17. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 17
  18. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 18
  19. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 19
  20. Slices - slice slice := []string{"a", "b", "c", "d", "e",

    "f"} // [a b c d e f] slice[2:5] // [c d e] slice[2:] // [c d e f] slice[:5] // [a b c d e] 20
  21. Slices - slice slice := []string{"a", "b", "c", "d", "e",

    "f"} // [a b c d e f] slice[2:5] // [c d e] slice[2:] // [c d e f] slice[:5] // [a b c d e] 21
  22. Slices - slice slice := []string{"a", "b", "c", "d", "e",

    "f"} // [a b c d e f] slice[2:5] // [c d e] slice[2:] // [c d e f] slice[:5] // [a b c d e] 22
  23. Slices - slice slice := []string{"a", "b", "c", "d", "e",

    "f"} // [a b c d e f] slice[2:5] // [c d e] slice[2:] // [c d e f] slice[:5] // [a b c d e] 23
  24. Maps m := make(map[string]int) m["foo"] = 1 // map[foo:1] m["bar"]

    = 2 // map[foo:1 bar:2] len(m) // 2 delete(m, "foo") // map[bar:2] val, ok := m["bar"] // 2, true 24
  25. Maps m := make(map[string]int) m["foo"] = 1 // map[foo:1] m["bar"]

    = 2 // map[foo:1 bar:2] len(m) // 2 delete(m, "foo") // map[bar:2] val, ok := m["bar"] // 2, true 25
  26. Maps m := make(map[string]int) m["foo"] = 1 // map[foo:1] m["bar"]

    = 2 // map[foo:1 bar:2] len(m) // 2 delete(m, "foo") // map[bar:2] val, ok := m["bar"] // 2, true 26
  27. Maps m := make(map[string]int) m["foo"] = 1 // map[foo:1] m["bar"]

    = 2 // map[foo:1 bar:2] len(m) // 2 delete(m, "foo") // map[bar:2] val, ok := m["bar"] // 2, true 27
  28. Maps m := make(map[string]int) m["foo"] = 1 // map[foo:1] m["bar"]

    = 2 // map[foo:1 bar:2] len(m) // 2 delete(m, "foo") // map[bar:2] val, ok := m["bar"] // 2, true 28
  29. Loops - classic while i := 0 for i <=

    3 { i = i + 1 } 29
  30. Loops - classic for for i := 0; i <=

    3; i++ { fmt.Println(i) } 30
  31. Loops - range numbers := []int{1, 2, 3} sum :=

    0 for i, val := range numbers { fmt.Println("index:", i) sum += val } fmt.Println("sum:", sum) 31
  32. Functions func sum(a int, b int) int { return a

    + b } // or func sum(a, b int) int { return a + b } 32
  33. Multiple return values func fancySum(a int, b int) (int, bool)

    { ok := true // some error checks return a + b, ok } func main() { sum, ok := fancySum(1, 2) if !ok { // fail with error } } 33
  34. Structs type person struct { firstname string lastname string age

    int } person1 := person{firstname: "John", lastname: "Doe", age: 28} person2 := person{"Jane", "Doe", 28} fmt.Println(person1.age) 34
  35. Structs composition type Model struct { ID int CreateDate time.Time

    ChangeDate time.Time } 35
  36. Structs composition type Model struct { ID int CreateDate time.Time

    ChangeDate time.Time } type User struct { Model Firstname string Lastname string } 36
  37. Structs composition user := User{ ID: 1, Firstname: "John", Lastname:

    "Doe", CreateDate: time.Now(), ChangeDate: time.Now(), } user.ID == user.Model.ID // true 37
  38. Interfaces type animal interface { color() string } 38

  39. Interfaces type cat struct { name string } type mouse

    struct { name string } 39
  40. Interfaces func (c cat) color() string { if c.name ==

    "Kitty" { return "black" } return "white" } func (m mouse) color() string { return "grey" } 40
  41. Interfaces func print(a animal) { fmt.Println(a.color()) } func main() {

    c1 := cat{"Kitty"} c2 := cat{"Miau"} m := mouse{"Pinky"} print(c1) // black print(c2) // white print(m) // grey } 41
  42. Hands On #1 calc 15 min 42

  43. Hands On #1 • Add • Sub • Mul •

    Div • Fib - iterativ 43
  44. Building an API • DB Access • HTTP + Routing

    • jsonapi encoding 44
  45. Hands On #2 Company API 30 min 45

  46. Company API • gorm.io • github.com/gorilla/mux • github.com/google/jsonapi { "data":

    { "id": 1, "type": "company", "attributes": { "name": "karriere.at", "zip": "4020", "city": "Linz" } } } 46
  47. Goroutines • concurrent execution of code • synchronization needed for

    shared resources (mutex, lock) 47
  48. Goroutines func doSomeWork() { for i := 0; i <

    5; i++ { fmt.Println("Work index:", i) } } func main() { go doSomeWork() // continue with main thread flow } 48
  49. Channels • used for communication between goroutines • sender sends

    messages, receiver reads from channel • channels are basically blocking except they are buffered 49
  50. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 50
  51. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 51
  52. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 52
  53. Demo Time Goroutines 53

  54. Tooling for Go • code formating • linting • testing

    • benchmarking • documentation • profiling 54
  55. my golang favorites • strong type system • error handling/multiple

    return values • implicit interfaces • built-in tooling • concurrency • speed 55
  56. Helpful resources • A Tour of Go https://tour.golang.org • Go

    by Example https://gobyexample.com • List of Go Books https://github.com/dariubs/GoBooks 56
  57. Thanks for listening @fetzi_io 57