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

[Michal Rostecki] Introduction to Golang

[Michal Rostecki] Introduction to Golang

Presentation from GDG DevFest Ukraine 2016.
Learn more at: https://devfest.gdg.org.ua

Google Developers Group Lviv

September 09, 2016
Tweet

More Decks by Google Developers Group Lviv

Other Decks in Technology

Transcript

  1. Copyright © 2015 Mirantis, Inc. All rights reserved What is

    Go? Go is a programming language, created by Rob Pike at Google, which is: • Statically typed • Compiled • Multithreaded • Garbage collected • Created mostly for networked systems, parallel computing and web applications
  2. Copyright © 2015 Mirantis, Inc. All rights reserved Hello world

    package main import "fmt" func main() { fmt.Println("Hello world") }
  3. Copyright © 2015 Mirantis, Inc. All rights reserved Variables foo

    := 3 bar := “something” var foo int foo = 3 var bar string bar = “something”
  4. Copyright © 2015 Mirantis, Inc. All rights reserved Arrays var

    a [5]int fmt.Println(a) // [0 0 0 0 0] a[2] = 100 fmt.Println(a) // [0 0 100 0 0]
  5. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1}
  6. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1} s = append(s, 2) // {0 1 2}
  7. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1} s = append(s, 2) // {0 1 2} s = append(s, 3) // {0 1 2 3}
  8. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1} s = append(s, 2) // {0 1 2} s = append(s, 3) // {0 1 2 3} fmt.Println(s[1:]) // {1 2 3}
  9. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1} s = append(s, 2) // {0 1 2} s = append(s, 3) // {0 1 2 3} fmt.Println(s[1:]) // {1 2 3} fmt.Println(s[:2]) // {0 1 2}
  10. Copyright © 2015 Mirantis, Inc. All rights reserved Slices s

    := make([]int, 1) // {0} s = append(s, 1) // {0 1} s = append(s, 2) // {0 1 2} s = append(s, 3) // {0 1 2 3} fmt.Println(s[1:]) // {1 2 3} fmt.Println(s[:2]) // {0 1 2} fmt.Println(s[1:2] // {1 2}
  11. Copyright © 2015 Mirantis, Inc. All rights reserved Slices x

    := make([]int, 1) // {0} y := make([]int, 0, 5) // {} with capacity 5
  12. Copyright © 2015 Mirantis, Inc. All rights reserved Slices Slices

    are resizable, despite having the length and capacity at the beginning. They support “slicing” operations (well known from Python), which get some defined part of slice.
  13. Copyright © 2015 Mirantis, Inc. All rights reserved What’s wrong

    with the other languages? • Inheritance, polymorphism, templating • Languages like C, C++ or Java are quite old • Dependencies in C and C++ scale very poorly • Python or Scala are easy and readable, but very slow • No system-based threading/concurrency in many standard libraries • Many modern languages don’t have pointers, only non-explicit references
  14. Copyright © 2015 Mirantis, Inc. All rights reserved What Go

    got rid of? • Inheritance • Classes • Exceptions (?)
  15. Copyright © 2015 Mirantis, Inc. All rights reserved Structs Defining

    structs is similar to C: type Foo struct { Name string Quantity int }
  16. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in C In C you define functions which get the instance of struct as an argument.
  17. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in C struct foo { char *name; int quantity; }
  18. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in C char *hello(struct foo) { char dest[256]; sprintf(dest, “Hello %s!”, foo); return dest; }
  19. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in Go In Go you don’t receive the struct as an argument. Writing methods for structs is very similar to writing methods for classes in object-oriented languages.
  20. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in Go type Foo struct { Name string Quantity int }
  21. Copyright © 2015 Mirantis, Inc. All rights reserved Struct methods

    in Go func (f Foo) Hello() string { return fmt.Sprintf(“Hello %s!”, foo.Name) }
  22. Copyright © 2015 Mirantis, Inc. All rights reserved Value vs

    pointer myPointer := &SomeStruct{} myValue := *myPointer
  23. Copyright © 2015 Mirantis, Inc. All rights reserved Value receiver

    vs pointer receiver Methods can have two kind of receivers.
  24. Copyright © 2015 Mirantis, Inc. All rights reserved Value receiver

    func (f Foo) SomeMethod() { f.Name = “new value” // This will not modify anything! }
  25. Copyright © 2015 Mirantis, Inc. All rights reserved Pointer receiver

    func (f *Foo) SomeMethod() { f.Name = “new value” }
  26. Copyright © 2015 Mirantis, Inc. All rights reserved Value receivers

    vs pointer receivers Value receiver will not modify the contents, because it receives the copy of the struct. Pointer receiver will modify the contents, because it has access to the original object in the memory.
  27. Copyright © 2015 Mirantis, Inc. All rights reserved Value receiver

    vs pointer receiver You can use both kind of receivers regardless of what kind of variable you have. That means that both: f = Foo{} f.SomeMethod() and f = &Foo{} f.SomeMethod() will work. However, it’s not recommended to mix pointer and value receivers in one struct, one of the next slides will explain why.
  28. Copyright © 2015 Mirantis, Inc. All rights reserved Composition vs

    inheritance Composition is “has-a” relationship. The object A can contain the object B. Type of A can contain a property of type of B. Inheritance is “is-a” relationship. The object A can have the same properties as the object B. Type of A is a subset of type of B.
  29. Copyright © 2015 Mirantis, Inc. All rights reserved Composition type

    FooCommons struct { Name string Quantity int }
  30. Copyright © 2015 Mirantis, Inc. All rights reserved Composition type

    Foo struct { Commons *FooCommons Annotations string[] }
  31. Copyright © 2015 Mirantis, Inc. All rights reserved Composition type

    Bar struct { Commons *FooCommons Weight int }
  32. Copyright © 2015 Mirantis, Inc. All rights reserved Inheritance class

    Base { protected: string name; int quantity; }
  33. Copyright © 2015 Mirantis, Inc. All rights reserved Inheritance class

    Foo: public Base { protected: std::vector<string> annotations; }
  34. Copyright © 2015 Mirantis, Inc. All rights reserved Interfaces Interface

    is a set of method signatures which may be implemented by some structure. Unlike in Java, you don’t declare the interface implementation explicitly.
  35. Copyright © 2015 Mirantis, Inc. All rights reserved Interfaces func

    (f Foo) Hello() string { return fmt.Sprintf(“Hello %s”, f.Name) }
  36. Copyright © 2015 Mirantis, Inc. All rights reserved Interfaces func

    main() { var f Helloer f = Foo{Name: “foo”} fmt.Println(f.Hello()) }
  37. Copyright © 2015 Mirantis, Inc. All rights reserved Pointers and

    values matter in interfaces // Pointer receiver func (f *Foo) Hello() string { return fmt.Sprintf(“Hello %s”, f.Name) }
  38. Copyright © 2015 Mirantis, Inc. All rights reserved Pointers and

    values matter in interfaces func main() { /* that will fail, because pointer impelents interface */ var h Helloer = Foo{} }
  39. Copyright © 2015 Mirantis, Inc. All rights reserved Pointers and

    values matter in interfaces func main() { var h Helloer = &Foo{} // that will work }
  40. Copyright © 2015 Mirantis, Inc. All rights reserved The empty

    interface interface{} means the same as void* in C. This function can take every type as an argument: func Foo(bar interface{}) { }
  41. Copyright © 2015 Mirantis, Inc. All rights reserved Type assertions

    var i interface{} = “something” s := i.(string)
  42. Copyright © 2015 Mirantis, Inc. All rights reserved Type assertions

    var i interface{} = “something” s := i.(string) s, ok := i.(string)
  43. Copyright © 2015 Mirantis, Inc. All rights reserved Type assertions

    var i interface{} = “something” s := i.(string) s, ok := i.(string) s, ok := i.(float64) // ok will be false
  44. Copyright © 2015 Mirantis, Inc. All rights reserved Type assertions

    var i interface{} = “something” s := i.(string) s, ok := i.(string) s, ok := i.(float64) // ok will be false if !ok { fmt.Printf(“it’s not float!”) }
  45. Copyright © 2015 Mirantis, Inc. All rights reserved Errors error

    is a type you can return in your function, check for it when calling the other functions and handle it in appropriate place.
  46. Copyright © 2015 Mirantis, Inc. All rights reserved Errors func

    DoSomethingWithNumber(n int) (*int, error) { If n == 10 { return nil, fmt.Errorf(“Cannot work with number 10”) } result := n + 2 return &result, nil }
  47. Copyright © 2015 Mirantis, Inc. All rights reserved Errors func

    main() { for _, i := range []int{2, 10, 24} { n, err := DoSomethingWithNumber(i) if err != nil { fmt.Fprintln(os.Stderr, “Error occured %#v”, err) } } }
  48. Copyright © 2015 Mirantis, Inc. All rights reserved Goroutines By

    writing go someFunction(arg1, arg2, …) You call this function as a goroutine, which is a thread with shared memory.
  49. Copyright © 2015 Mirantis, Inc. All rights reserved Goroutines func

    say(msg string) { time.Sleep(100) fmt.Println(msg) }
  50. Copyright © 2015 Mirantis, Inc. All rights reserved Goroutines func

    main() { go say(“world”) fmt.Println(“hello”) // “world” will be printed before “hello” }
  51. Copyright © 2015 Mirantis, Inc. All rights reserved Channels Channel

    is a type through which you can send and receive values.
  52. Copyright © 2015 Mirantis, Inc. All rights reserved Channels func

    foo(messages chan string) { messages <- “ping” }
  53. Copyright © 2015 Mirantis, Inc. All rights reserved Channels func

    main() { messages := make(chan string) go foo(messages) msg := <- messages fmt.Println(msg) // will print “ping” }
  54. Copyright © 2015 Mirantis, Inc. All rights reserved Interested in

    Golang? This presentation was just a brief outlook and introduction. If you want to play with Go, take the http://tour.golang.org!