1. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 1/54
Go - Introduction
October 2017
2. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 2/54
Go
package main
import "fmt"
func main() {
fmt.Println("Go - Introductionnn")
for _, speaker := range []string{"Tomasz Smelcerz", "Piotr Miśkiewicz", "Piotr Mścichowski"} {
fmt.Println(speaker)
}
} Run
3. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 3/54
Speakers
Piotr Miśkiewicz
piotr.miskiewicz (at) sap.com
Piotr Mścichowski
piotr.mscichowski (at) hybris.com
Tomasz Smelcerz
tomasz.smelcerz (at) hybris.com
4. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 4/54
Brought to you by Hack Your Career
www.facebook.com/Hack.your.Career(https://www.facebook.com/Hack.your.Career)
5. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 5/54
Agenda
Go key concepts
Where to use Go
Code examples - part 1
Why Go was created
Who's using Go
Code examples - part 2
Summary
6. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 6/54
What's in it for me ?
7. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 7/54
What's in it for me ?
insights.stackover ow.com/survey/2017#top-paying-technologies(https://insights.stackover ow.com/survey/2017#top-
paying-technologies)
8. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 8/54
9. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 9/54
What's in it for me ?
insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted
(https://insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted)
11. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 11/54
What for ?
For applications where high performance matters
OS applications
Network applications
Backend applications
12. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 12/54
Go examples - part 1
We'll just give you an overview of the most important features
For more detailed info, see:
- tour.golang.org/welcome/1(https://tour.golang.org/welcome/1)
- gobyexample.com(https://gobyexample.com)
17. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 17/54
Error Handling
The error type is an interface type.
An error variable represents any value that can describe itself as a string.
Here is the interface's declaration:
type error interface {
Error() string
}
18. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 18/54
Interfaces
Interfaces are named collections of method signatures.
We Use them to de ne contracts (like in Java)
We can implement an interface on objects of other types (usually structs)
19. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 19/54
Duck Typing in Go
"If it walks like a duck, and it quacks like a duck, then it is a duck"
No need to declare that a type implements an interface
All you need is to implement all the methods of the interface (on some type)
In go structs, simple types and even function types can implement interfaces
20. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 20/54
Interface implementation example
8 //start
9 type shape interface {
10 Area() float64
11 }
12
13 func describe(s shape) {
14 fmt.Println("My area is ", s.Area())
15 }
16
17 type circle struct {
18 radius float64
19 }
20
21 //This is how we define method on a struct
22 func (c circle) Area() float64 {
23 return math.Pi * c.radius * c.radius
24 }
25
26 func main() {
27 describe(circle{radius: 10})
28 }
29
30 //end Run
21. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 21/54
More on structs
1 package main
2
3 import "fmt"
4
5 type person struct {
6 FirstName string
7 LastName string
8 Age int
9 }
10
11 //Define method on the struct
12 func (p *person) Speak() {
13 fmt.Printf("Hello, I'm %v %v and I am %v years oldn", p.FirstName, p.LastName, p.Age)
14 }
15
16 func main() {
17 john := person{"John", "Doe", 25}
18
19 //invoke method on the struct
20 john.Speak()
21 } Run
22. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 22/54
Is golang an O/O language then?
From o cial GO FAQ: Yes and no :)
Although Go has types and methods and allows an object-oriented style of programming,
there is no type hierarchy.
Moreover, methods in Go are more general than in C++ or Java: they can be de ned for any
sort of data, even built-in types such as plain, “unboxed” integers.
They are not restricted to structs (classes).
23. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 23/54
Type system summary
No generic types, no type hierarchy
built-in (simple) types: bool, int, int64, string...
(BTW: string is NOT pointer type, zero value for string is ""!)
interfaces
structs
functions
pointers
24. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 24/54
Pointers...
1 package main
2
3 import "fmt"
4
5 func main() {
6 i, j := 42, 2701
7
8 p := &i // point to i
9 fmt.Println(*p) // read i through the pointer
10 *p = 21 // set i through the pointer
11 fmt.Println(i) // see the new value of i
12
13 p = &j // point to j
14 *p = *p / 37 // divide j through the pointer
15 fmt.Println(j) // see the new value of j
16 } Run
25. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 25/54
Pointers, cont.
There is no pointer arithmetic.
Used for arguments of functions/methods
Since Go uses "pass-by-value", use pointers to avoid copying data
Because strings are NOT pointers, chance of nil pointer errors is reduced compared to
e.g: Java
26. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 26/54
Any questions so far?
27. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 27/54
Compilation
Go compiles to a single statically-linked executable binary
Go binary includes run-time library but it's NOT a VM, it runs native code
Go run-time library is handling things like GC and goroutines support.
You can cross-compile to a di erent OS (linux, darwin, windows) and arch (x86, arm)
28. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 28/54
Tools
Go comes with a complete productivity toolset: go <arg> :
build compile packages and dependencies
clean remove object files
doc show documentation for package or symbol
fmt run gofmt on package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
version print Go version
vet run go tool vet on packages
And this presentation is done with present tool:
go get golang.org/x/tools/cmd/present
29. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 29/54
Why Go was created ? Leo why ?!
30. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 30/54
Why Go was created ? Leo why ?!
E cient Compilation
E cient & simple execution
Ease of programming
31. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 31/54
What does Go give us ?
simplicity
performance
consistency
fast builds
support for concurrent programming
32. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 32/54
Who uses Go?
33. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 33/54
Who uses Go?
Docker
34. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 34/54
Who uses Go?
Twitter
35. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 35/54
Who uses Go?
Uber
36. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 36/54
Who uses Go?
SpaceX
37. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 37/54
Who uses Go?
Many others like :
CoreOS
Adobe
Dropbox
In uxDB
nd out more at :
github.com/golang/go/wiki/GoUsers(https://github.com/golang/go/wiki/GoUsers)
38. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 38/54
Go examples - part 2
Defer
Concurrency
Unit tests
39. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 39/54
Defer
What is the result?
package main
import "fmt"
func main() {
defer fmt.Println("Hello")
fmt.Println("World")
} Run
40. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 40/54
Defer part 2
package main
import "os"
import "fmt"
func main() {
f, err := os.Open("abc.txt")
if err != nil {
fmt.Println("Got error", err)
return
}
defer f.Close()
f.WriteString("data")
} Run
41. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 41/54
Finally in Python
try:
f = open(filePath, 'w')
except IOError:
msg = ("Unable to create file on disk.")
f.close()
return
finally:
if f != None:
f.write("Hello World!")
f.close()
42. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 42/54
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
package main
import (
"time"
"fmt"
)
func sayHello(name string) {
for i := 0; i < 3; i++ {
time.Sleep(50 * time.Millisecond)
fmt.Println("hello ", name)
}
}
func main() {
go sayHello("Piotr")
sayHello("Tomasz")
} Run
43. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 43/54
Concurrecny in Go vs Java
go func() {
fmt.Println("Hello")
}()
new Thread(() -> {
System.out.println("Hello from Thread");
}).start();
44. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 44/54
Million goroutines
package main
import "sync"
import "time"
import "fmt"
import "math/rand"
func main() {
wg := sync.WaitGroup{}
bef := time.Now()
const len = 1000000 // million
wg.Add(len)
var out [len]int32
for i := 0; i < len; i++ {
go func(idx int) {
out[idx] = rand.Int31n(1000)
wg.Done()
}(i)
}
wg.Wait()
fmt.Println(time.Now().Sub(bef))
} Run
45. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 45/54
Channels
Do not communicate by sharing memory; instead, share memory by communicating.
46. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 46/54
47. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 47/54
Channels - example
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
// consumer
go func() {
for {
v := <-ch
fmt.Println(v)
time.Sleep(500 * time.Millisecond)
}
}()
for i := 0; i < 10; i ++ {
ch <- i
}
} Run
48. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 48/54
Channels - timeout
package main
import (
"time"
"fmt"
)
func main() {
ch := make(chan int)
go func() {
time.Sleep(100 * time.Millisecond)
ch <- 1
}()
select {
case v := <- ch:
fmt.Println("Received from channel", v)
case <- time.After(150 * time.Millisecond):
fmt.Println("Timeout")
}
} Run
49. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 49/54
Testing
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func sum(a, b int) int {
return a + b
}
func TestSum(t *testing.T) {
if sum(2, 3) != 5 {
t.Errorf("failed")
}
}
func TestSumWithAssertions(t *testing.T) {
assert.Equal(t, 5, sum(2, 3))
}
50. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 50/54
Table testing
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func sum(a, b int) int {
return a + b
}
func TestSumTables(t *testing.T) {
for _, tc := range []struct {
A int
B int
Exp int
} {
{A: 1, B: 2, Exp: 3}, {A: 10, B: 2, Exp: 12}, {A: -10, B: 2, Exp: -8},
} {
t.Run("", func(t *testing.T) {
assert.Equal(t, tc.Exp, sum(tc.A, tc.B))
})
}
}
51. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 51/54
Summary
Key concepts
What does Go give us?
Why should we learn Go
Welcome to next Go presentations
52. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 52/54
Survey
www.surveymonkey.com/r/H7MNM7H(https://www.surveymonkey.com/r/H7MNM7H)
53. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 53/54
Thank you
54. 19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 54/54