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

Einführung in Go

Einführung in Go

Eine kurze Einführung in die Programmiersprache Go und dessen Konzepte.

Mario Behrendt

January 14, 2014

Transcript

50 slides
  1. Go

  2. Was ist Go? • Stark, statisch typisiert • Kompiliert (schnell!)

    zu Maschinencode • Garbage collected • Concurrency: Goroutinen & Channels
  3. Was ist Go? • Stark, statisch typisiert • Kompiliert (schnell!)

    zu Maschinencode • Garbage collected • Concurrency: Goroutinen & Channels • Simple Sprache, riesige Standard-Lib (Flags, Img)
  4. Was ist Go? • Stark, statisch typisiert • Kompiliert (schnell!)

    zu Maschinencode • Garbage collected • Concurrency: Goroutinen & Channels • Simple Sprache, riesige Standard-Lib (Flags, Img) • OOP
  5. package main ! import "fmt" ! type Printable interface {

    PrintName() } ! type A struct { Name string } ! func (a A) PrintName() { fmt.Println("A has name: " + a.Name) } ! type B struct { Name string } ! func (b B) PrintName() { fmt.Println("B has name: " + b.Name) } ! func printSomethingsName(p Printable) { p.PrintName() } ! func main() { A{"John"}.PrintName() printSomethingsName(B{"James"}) }
  6. package main ! import "fmt" ! type A struct {

    ! } ! type B struct { A } ! func (a A) Foo() { fmt.Println("Foo") } ! func main() { B{}.Foo() }
  7. Concurrency in Go package main ! import ( "fmt" "time"

    "strconv" ) ! func printSeconds(i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") } ! func main() { for i:=1; i < 6; i++ { printSeconds(i) } }
  8. Concurrency in Go package main ! import ( "fmt" "time"

    "strconv" ) ! func printSeconds(i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") } ! func main() { for i:=1; i < 6; i++ { printSeconds(i) } } package main ! import ( "fmt" "time" "strconv" ) ! func printSeconds(i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") } ! func main() { for i:=1; i < 6; i++ { go printSeconds(i) } time.Sleep(2 * time.Second) }
  9. Concurrency in Go package main ! import ( "fmt" "time"

    "strconv" ) ! func printSeconds(i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") } ! func main() { for i:=1; i < 6; i++ { go printSeconds(i) } time.Sleep(2 * time.Second) }
  10. Concurrency in Go package main ! import ( "fmt" "time"

    "strconv" ) ! func printSeconds(i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") } ! func main() { for i:=1; i < 6; i++ { go printSeconds(i) } time.Sleep(2 * time.Second) } package main ! import ( "fmt" "time" "strconv" ) ! func printSeconds(c chan int, i int) { time.Sleep(1 * time.Second) fmt.Println(strconv.Itoa(i) + " seconds") c <- 1 } ! func main() { c := make(chan int) ! for i:=1; i < 6; i++ { go printSeconds(c, i) } ! <- c }
  11. package main ! import ( "fmt" "log" "time" e "errors"

    "math/rand" "net/http" ) ! func multipleReturns() (error, bool) { rand.Seed(time.Now().UTC().UnixNano()) ! if rand.Intn(10) % 2 == 0 { return nil, true } else { return e.New("Failed"), false } } ! func main() { err, _ := multipleReturns() ! if err != nil { log.Fatal(err) } ! fmt.Println("Random number was even! Starting web server...") ! http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello!") }) ! http.ListenAndServe(":4000", nil) }
  12. Go tool chain • go build & go run •

    go get • go fmt • go test
  13. Go tool chain • go build & go run •

    go get • go fmt • go test • go tool
  14. Go tool chain • go build & go run •

    go get • go fmt • go test • go tool
  15. Go tool chain • go build & go run •

    go get • go fmt • go test • go tool • go doc
  16. Negative Seiten • go get unterstützt im Moment keine Versionen

    • Memory-leaks • Community-Libs leiden unter Node.JS-Effekt
  17. Negative Seiten • go get unterstützt im Moment keine Versionen

    • Memory-leaks • Community-Libs leiden unter Node.JS-Effekt • Keine Generics - bis jetzt
  18. Negative Seiten • go get unterstützt im Moment keine Versionen

    • Memory-leaks • Community-Libs leiden unter Node.JS-Effekt • Keine Generics - bis jetzt • Error-Handling äußerst verbose
  19. Negative Seiten • go get unterstützt im Moment keine Versionen

    • Memory-leaks • Community-Libs leiden unter Node.JS-Effekt • Keine Generics - bis jetzt • Error-Handling äußerst verbose • (Keine Vererbung)
  20. Wer nutzt Go? • Google :) • Shopify • Heroku

    • Soundcloud • Bitly • Canonical

Other Decks in Programming