Slide 1

Slide 1 text

Hallo! Thorsten Ball mrnugget / @thorstenball Software Developer

Slide 2

Slide 2 text

@thorstenball Go Go

Slide 3

Slide 3 text

@thorstenball Go Go’s Erfinder • Robert Griesemer (V8 Engine, HotSpot Java VM) • Rob Pike (Plan 9 Unix, UTF-8) • Ken Thompson (Unix, B, UTF-8)

Slide 4

Slide 4 text

@thorstenball Go Ziele • "It must work at scale, for large programs with large numbers of dependencies, with large teams of programmers working on them.” • "It must be familiar, roughly C-like. [...] The need to get programmers productive quickly” • "It must be modern. [...] built in concurrency"

Slide 5

Slide 5 text

@thorstenball Go Go • 2007 - erste Entwürfe • 2009 - erste Version veröffentlicht • 2012 - Version 1.0 veröffentlicht • 2015 - Version 1.4

Slide 6

Slide 6 text

@thorstenball Go Go • Kompiliert • Statisch • Garbage Collection • Concurrency • Schnell

Slide 7

Slide 7 text

@thorstenball Go Wie sieht Go aus?

Slide 8

Slide 8 text

@thorstenball Go Hello World package main import "fmt" func main() { fmt.Println("Hello World!") }

Slide 9

Slide 9 text

@thorstenball Go Funktionen package main import "fmt" func SayHello(name string) { fmt.Println("Hello " + name) } func main() { SayHello("World!") SayHello("Go!") SayHello("flinc!") }

Slide 10

Slide 10 text

@thorstenball Go Variablen package main import "fmt" var i int = 1 var name string = "Go" func main() { var x int = 2 var y = 3 z := 4 s := "bob" fmt.Println(i, name, x, y, z, s) }

Slide 11

Slide 11 text

@thorstenball Go Flow-Control package main import "fmt" func main() { for i := 0; i < 10; i++ { if i % 2 == 0 { fmt.Println(i) } else { fmt.Println("no") } } // while == for sum := 1 for sum < 10 { sum += sum } }

Slide 12

Slide 12 text

@thorstenball Go Structs package main import "fmt" type Person struct { Age int Name string } func main() { var bob Person bob.Age = 24 bob.Name = "Bob" anna := Person{Age: 27, Name: "Anna"} fmt.Printf("Name: %s, Age: %d\n", bob.Name, bob.Age) fmt.Printf("Name: %s, Age: %d\n", anna.Name, anna.Age) }

Slide 13

Slide 13 text

@thorstenball Go Pointers! package main import "fmt" type Person struct { Name string } func SayName(p *Person) { fmt.Println(p.Name) } func main() { anna := &Person{Age: 27, Name: "Anna"} SayName(anna) }

Slide 14

Slide 14 text

@thorstenball Go Programmieren mit Go

Slide 15

Slide 15 text

@thorstenball Go Die Umgebung • $GOPATH • Source, Binary und Executable • Normale Verzeichnisse, keine “magic” • Verwaltung mit go-Tool

Slide 16

Slide 16 text

@thorstenball Go $GOPATH bin/ hello # command executable outyet # command executable pkg/ linux_amd64/ github.com/golang/example/ stringutil.a # package object src/ github.com/golang/example/ .git/ # Git repository metadata hello/ hello.go # command source outyet/ main.go # command source main_test.go # test source stringutil/ reverse.go # package source reverse_test.go # test source

Slide 17

Slide 17 text

@thorstenball Go Tools • Go hat viele Tools um mit Go-Code zu arbeiten • Der Großteil ist in der Standard-Installation enthalten • Tools sind sehr wichtig bei Go • Keine zusätzlichen Installationen nötig

Slide 18

Slide 18 text

@thorstenball Go go Tool • go get • go build • go install • go test • go fmt • go test -race • go vet • go tool pprof

Slide 19

Slide 19 text

@thorstenball Go go fmt

Slide 20

Slide 20 text

@thorstenball Go Standard Library • Sehr groß • Sehr gut gepflegt • Der idiomatischste Go-Code (/usr/local/go/src)

Slide 21

Slide 21 text

@thorstenball Go Standard Library • net/http - HTTP-Server und Client • io - I/O-Interfaces und Hilfsfunktionen • fmt - Formattiertes IO (Printf, Println, Sprintf) • flag - Command-Line Arguments • database/sql - Grundgerüst für SQL-Treiber • encoding/json - JSON-Parser • html/template - HTML-Templating Engine • …

Slide 22

Slide 22 text

@thorstenball Go Packages

Slide 23

Slide 23 text

@thorstenball Go Packages package foobar import "fmt" func Hello() { fmt.Println("Hello World!") }

Slide 24

Slide 24 text

@thorstenball Go Packages package main import "github.com/mrnugget/foobar" func main() { foobar.Hello() }

Slide 25

Slide 25 text

@thorstenball Go Packages • GroßGeschriebene Namen werden exportiert • kleinGeschriebene Namen werden nicht exportiert • Kein Monkey-Patching

Slide 26

Slide 26 text

@thorstenball Go Package-Management

Slide 27

Slide 27 text

@thorstenball Go Testing • Test-Framework ist mitgeliefert • import “testing” • foobar_test.go • go test

Slide 28

Slide 28 text

@thorstenball Go Testing package foobar import "testing" func TestSayName(t *testing.T) { result := SayName() if result != "thorsten" { t.Error("result is wrong") } }

Slide 29

Slide 29 text

@thorstenball Go Keine Klassen, keine Vererbung • Structs • Types • Methoden auf Structs/Types definierbar • First-class functions!

Slide 30

Slide 30 text

@thorstenball Go Concurrency

Slide 31

Slide 31 text

@thorstenball Go goroutines package main import "fmt" import "time" func ThreeTimes(line string) { for i := 0; i < 3; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(line) } } func main() { go ThreeTimes("hello") ThreeTimes("world") } // Output: world hello world hello world

Slide 32

Slide 32 text

@thorstenball Go Channels

Slide 33

Slide 33 text

@thorstenball Go Channels package main import "fmt" import "math/rand" import "time" func CalcNumber(results chan int) { time.Sleep(100 * time.Millisecond) results <- rand.Int() // blocking } func main() { results := make(chan int) go CalcNumber(results) go CalcNumber(results) num := <-results // blocking fmt.Println(num) num = <-results // blocking fmt.Println(num) }

Slide 34

Slide 34 text

@thorstenball Go Wofür?

Slide 35

Slide 35 text

@thorstenball Go Wofür? • Web-Services • Infrastruktur • Server • Command Line Tools

Slide 36

Slide 36 text

@thorstenball Go Go wird eingesetzt von… • Google • Apple • Microsoft • Dropbox • GitHub • Shopify • Soundcloud • Heroku • Tumblr • 6Wunderkinder

Slide 37

Slide 37 text

@thorstenball Go Go.

Slide 38

Slide 38 text

@thorstenball Go Go lernen • tour.golang.org • golang.org/doc • blog.golang.org • Google “golang” (…)

Slide 39

Slide 39 text

@thorstenball Go Danke