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

Go 1.5 & 1.6の新機能のおさらい

Ian Lewis
December 01, 2015

Go 1.5 & 1.6の新機能のおさらい

Goは実運用の事例が最近多く出ています。
なんで最近実用的に使っている開発者が増えているのでしょうか。
実は互換性を待ちながら、便利な機能が
たくさん出てくるのがその理由の一つです。
9月にリリースされたGo 1.5と開発中のGo 1.6の新しい機能を紹介します。

Ian Lewis

December 01, 2015
Tweet

More Decks by Ian Lewis

Other Decks in Technology

Transcript

  1. Ian Lewis
    Developer Advocate
    Google Cloud Platform
    Go 1.5 & 1.6の新機能のおさらい

    View Slide

  2. Confidential & Proprietary
    Google Cloud Platform 2
    Ian Lewis
    Developer Advocate, Google Cloud Platform
    Tokyo, Japan
    @IanMLewis
    google.com/+IanLewis-hoge

    View Slide

  3. Confidential & Proprietary
    Google Cloud Platform 3
    Go!
    Image © Takuya Ueda. Licensed under CC BY 3.0.

    View Slide

  4. Confidential & Proprietary
    Google Cloud Platform 4
    Go(lang)
    ● Google で起こる問題解決するために
    Googleで開発
    ○ 開発スピード
    ○ 使いにくい型システム
    ○ 並行処理

    View Slide

  5. Confidential & Proprietary
    Google Cloud Platform 5
    Goの機能
    ● ネイティブコードにコンパイル
    ● ガーメージコレックション
    ● 型付き
    ● Goroutines
    ● Channels
    ● Structs/Interfaces

    View Slide

  6. Confidential & Proprietary
    Google Cloud Platform 6
    Goroutines & Channels
    Channel
    Goroutine Goroutine

    View Slide

  7. Confidential & Proprietary
    Google Cloud Platform 7
    Goroutines & Channels
    Channel
    Goroutine Goroutine
    Channel

    View Slide

  8. Confidential & Proprietary
    Google Cloud Platform 8
    Goroutines & Channels
    func sum(a []int, c chan int) {
    sum := 0
    for _, v := range a {
    sum += v
    }
    c <- sum // send sum to c
    }

    View Slide

  9. Confidential & Proprietary
    Google Cloud Platform 9
    Goroutines & Channels
    func main() {
    a := []int{7, 2, 8, -9, 4, 0}
    c := make(chan int)
    go sum(a[:len(a)/2], c)
    go sum(a[len(a)/2:], c)
    x, y := <-c, <-c // receive from c
    fmt.Println(x, y, x+y)
    }

    View Slide

  10. Confidential & Proprietary
    Google Cloud Platform 10
    事例
    Image © Takuya Ueda. Licensed under CC BY 3.0.

    View Slide

  11. Confidential & Proprietary
    Google Cloud Platform 11
    Docker
    ● コンテナランタイムエンジン
    ● HTTP API サーバー
    ● イメージパケージフォーマット
    Image © Docker All Rights Reserved
    http://docs.docker.com/

    View Slide

  12. Confidential & Proprietary
    Google Cloud Platform 12
    etcd
    ● Distributed Key-Value Store
    ● Used for Storing Cluster State/Config
    ● Implements RAFT Protocol

    View Slide

  13. Confidential & Proprietary
    Google Cloud Platform 13
    Vitess
    ● Used at Youtube
    ● Sharding MySQL Servers
    ● Serving all YouTube DB traffic
    since 2011
    ● http://vitess.io/

    View Slide

  14. Confidential & Proprietary
    Google Cloud Platform 14
    Kubernetes
    ● Container Cluster Manager
    ● Manages a Distributed Cluster
    ● Supports Pods
    ● Provides HTTP API

    View Slide

  15. Confidential & Proprietary
    Google Cloud Platform 15
    Gorilla
    ● ウェブツールキット
    ● リクエストコンテキスト
    ● セッション管理
    ● URLルーティング
    ● URLリバース
    ● Web Sockets

    View Slide

  16. Confidential & Proprietary
    Google Cloud Platform 16
    Go 1.5
    Image © Takuya Ueda. Licensed under CC BY 3.0.

    View Slide

  17. Confidential & Proprietary
    Google Cloud Platform 17
    Go 1.5
    ● Released Aug 2015
    ● Go 1.0から、一番大きいリリース
    ● 新しい並行GC
    ● Go コンパイラやツールチェインの改善
    ● パッケージベンダリング (beta)
    ● goroutineスケジューラーの改善

    View Slide

  18. Confidential & Proprietary
    Google Cloud Platform 18
    並行GC
    ● ユーザープログラムと並行に動く
    ● プログラムを停止する時間を短めに
    (<10ms)

    View Slide

  19. Confidential & Proprietary
    Google Cloud Platform 19
    並行GC

    View Slide

  20. Confidential & Proprietary
    Google Cloud Platform 20
    並行GC

    View Slide

  21. Confidential & Proprietary
    Google Cloud Platform 21
    並行GC

    View Slide

  22. Confidential & Proprietary
    Google Cloud Platform 22
    並行GC

    View Slide

  23. Confidential & Proprietary
    Google Cloud Platform 23
    並行GC

    View Slide

  24. Confidential & Proprietary
    Google Cloud Platform 24
    並行GC
    ● ユーザープログラムと並行に動く
    ● プログラムを停止する時間を短めに
    (<10ms)

    View Slide

  25. Confidential & Proprietary
    Google Cloud Platform 25
    Go コンパイラやツールチェインの改善
    ● GoはGoで書かれている
    ● GoをビルドするにはCコンパイラーはいらな

    ● Go開発はシンプルになる

    View Slide

  26. Confidential & Proprietary
    Google Cloud Platform 26
    Go コンパイラやツールチェインの改善
    ● 新しい go tool trace コマンド
    ● Goシェアドライブラリー サポート

    View Slide

  27. Confidential & Proprietary
    Google Cloud Platform 27
    Go コンパイラやツールチェインの改善
    $ go test -o pkg.test net/http -trace trace.out
    ok net/http 20.876s
    $ go tool trace pkg.test trace.out

    View Slide

  28. Confidential & Proprietary
    Google Cloud Platform 28
    Goパッケージベンダリング
    ● パッケージベンダリング
    ○ GO15VENDOREXPERIMENT=1
    ○ vendor ディレクトリに依存ライブラリを入
    れる

    View Slide

  29. Confidential & Proprietary
    Google Cloud Platform 29
    Goパッケージベンダリング
    hoge/
    hoge.go
    vendor/piyo/
    piyo.go
    fuga/
    fuga.go

    View Slide

  30. Confidential & Proprietary
    Google Cloud Platform 30
    Goパッケージベンダリング
    hoge/
    hoge.go
    vendor/piyo/
    piyo.go
    fuga/
    fuga.go
    piyo/
    piyo.go

    View Slide

  31. Confidential & Proprietary
    Google Cloud Platform 31
    Goパッケージベンダリング
    ● 特定なバージョンを部分的に使う
    ● 分かりやすくて簡単
    ● ベンダリングするツールがいくつか (Godeps
    など)

    View Slide

  32. Confidential & Proprietary
    Google Cloud Platform 32
    Go 1.6
    Image © Takuya Ueda. Licensed under CC BY 3.0.

    View Slide

  33. Confidential & Proprietary
    Google Cloud Platform 33
    Go 1.6
    ● Release Jan 2016?
    ● パッケージベンダリング (stable)
    ● HTTP/2 サポート (net/http)
    ● Text/HTMLテンプレートのコード
    ブロック
    ● メモリサニタイザー

    View Slide

  34. Confidential & Proprietary
    Google Cloud Platform 34
    HTTP/2 Old
    // hello world, the web server
    func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "hello, world!\n")
    }
    func main() {
    m := http.NewServeMux()
    m.HandleFunc("/", HelloServer)
    srv := &http.Server{
    Addr: ":8000", // Normally ":443"
    Handler: m,
    }
    http2.ConfigureServer(srv, &http2.Server{})
    log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
    }

    View Slide

  35. Confidential & Proprietary
    Google Cloud Platform 35
    HTTP/2
    // hello world, the web server
    func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "hello, world!\n")
    }
    func main() {
    http.HandleFunc("/", HelloServer)
    log.Fatal(http.ListenAndServeTLS(
    ":8000", "server.crt", "server.key", nil))
    }

    View Slide

  36. Confidential & Proprietary
    Google Cloud Platform 36
    HTTP/2
    // hello world, the web server
    func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "hello, world!\n")
    }
    func main() {
    http.HandleFunc("/", HelloServer)
    log.Fatal(http.ListenAndServeTLS(
    ":8000", "server.crt", "server.key", nil))
    }

    View Slide

  37. Confidential & Proprietary
    Google Cloud Platform 37
    テンプレートの block キーワード
    ● テンプレート継承が実現できる
    ● block を定義して、別のテンプレートで
    オーバーライドできる

    View Slide

  38. Confidential & Proprietary
    Google Cloud Platform 38
    テンプレートの block キーワード


    {{ block "title" .}}Default title{{end}}

    {{ block "body" .}}Default body{{end}}

    View Slide

  39. Confidential & Proprietary
    Google Cloud Platform 39
    テンプレートの block キーワード
    {{define "title"}}About page{{end}}
    {{define "body"}}About me{{end}}

    View Slide

  40. Confidential & Proprietary
    Google Cloud Platform 40
    テンプレートの block キーワード
    baseTmpl, err := template.New("base").Parse(base)
    if err != nil {
    log.Fatal(err)
    }
    aboutPageTmpl, err := template.Must(baseTmpl.Clone()).Parse(aboutPage)
    if err != nil {
    log.Fatal(err)
    }
    if err := aboutPageTmpl.Execute(os.Stdout, nil); err != nil {
    log.Fatal(err)
    }

    View Slide

  41. Goを使い尽く
    しましょう

    View Slide

  42. Confidential & Proprietary
    Google Cloud Platform 42
    Go を使い尽くしましょう
    ● Service Oriented Architecture
    ● サービス基盤
    ● コンテナ

    View Slide

  43. Confidential & Proprietary
    Google Cloud Platform 43
    Ian Lewis
    Developer Advocate, Google Cloud Platform
    Tokyo, Japan
    @IanMLewis
    google.com/+IanLewis-hoge
    Thank You!

    View Slide