$30 off During Our Annual Pro Sale. View Details »

Die Programmiersprache Go

Die Programmiersprache Go

Vorstellung von Go bei einem flinc Lunch-Talk. Was ist Go? Was kann Go? Wofür kann ich Go benutzen?

Thorsten Ball

February 11, 2015
Tweet

More Decks by Thorsten Ball

Other Decks in Programming

Transcript

  1. Hallo!
    Thorsten Ball
    mrnugget / @thorstenball
    Software Developer

    View Slide

  2. @thorstenball
    Go
    Go

    View Slide

  3. @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)

    View Slide

  4. @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"

    View Slide

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

    View Slide

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

    View Slide

  7. @thorstenball
    Go
    Wie sieht Go aus?

    View Slide

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

    View Slide

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

    View Slide

  10. @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)
    }

    View Slide

  11. @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
    }
    }

    View Slide

  12. @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)
    }

    View Slide

  13. @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)
    }

    View Slide

  14. @thorstenball
    Go
    Programmieren mit Go

    View Slide

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

    View Slide

  16. @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

    View Slide

  17. @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

    View Slide

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

    View Slide

  19. @thorstenball
    Go
    go fmt

    View Slide

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

    View Slide

  21. @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
    • …

    View Slide

  22. @thorstenball
    Go
    Packages

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. @thorstenball
    Go
    Package-Management

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  30. @thorstenball
    Go
    Concurrency

    View Slide

  31. @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

    View Slide

  32. @thorstenball
    Go
    Channels

    View Slide

  33. @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)
    }

    View Slide

  34. @thorstenball
    Go
    Wofür?

    View Slide

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

    View Slide

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

    View Slide

  37. @thorstenball
    Go
    Go.

    View Slide

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

    View Slide

  39. @thorstenball
    Go
    Danke

    View Slide