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

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?

Avatar for Thorsten Ball

Thorsten Ball

February 11, 2015
Tweet

More Decks by Thorsten Ball

Other Decks in Programming

Transcript

  1. @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)
  2. @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"
  3. @thorstenball Go Go • 2007 - erste Entwürfe • 2009

    - erste Version veröffentlicht • 2012 - Version 1.0 veröffentlicht • 2015 - Version 1.4
  4. @thorstenball Go Funktionen package main import "fmt" func SayHello(name string)

    { fmt.Println("Hello " + name) } func main() { SayHello("World!") SayHello("Go!") SayHello("flinc!") }
  5. @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) }
  6. @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 } }
  7. @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) }
  8. @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) }
  9. @thorstenball Go Die Umgebung • $GOPATH • Source, Binary und

    Executable • Normale Verzeichnisse, keine “magic” • Verwaltung mit go-Tool
  10. @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
  11. @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
  12. @thorstenball Go go Tool • go get • go build

    • go install • go test • go fmt • go test -race • go vet • go tool pprof
  13. @thorstenball Go Standard Library • Sehr groß • Sehr gut

    gepflegt • Der idiomatischste Go-Code (/usr/local/go/src)
  14. @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 • …
  15. @thorstenball Go Testing package foobar import "testing" func TestSayName(t *testing.T)

    { result := SayName() if result != "thorsten" { t.Error("result is wrong") } }
  16. @thorstenball Go Keine Klassen, keine Vererbung • Structs • Types

    • Methoden auf Structs/Types definierbar • First-class functions!
  17. @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
  18. @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) }
  19. @thorstenball Go Go wird eingesetzt von… • Google • Apple

    • Microsoft • Dropbox • GitHub • Shopify • Soundcloud • Heroku • Tumblr • 6Wunderkinder