Paxosは、信頼性の低い複数の処理ノードによるネットワークで「コンセンサス」を得るための各種手順Paxos自体は汎用的なアルゴリズムでさまざまな問題に適用できますが、最近のNoSQLの文脈で語られるのは「データストアをマルチマスター構成にしたときに、どうやってデータの整合性を効率的に確保するか」って問題へのPaxos応用です。Paxosは独立した並列なプロセスをメッセージパッシングで行うので、goroutine & channelがはまったwebsocketとか便利だった(いまは準標準パッケージ)無駄な議論をしなくて済むようになったデプロイがすごく楽簡潔に書けてとてもよい
29.
言語仕様 channel 非同期な処理同士のやり取りを行うための窓口 “Dont communicate by sharing memory, share memory by communicating.” – Rob Pike イメージ コード例 func ProcessA(c chan int) { Process A Process B … c <- SomeProcessA() … Channel C } func ProcessB(c chan int) { … SomeProcessB(<-c) … }http://talks.golang.org/2012/concurrency.slide 29
30.
言語仕様 channelfunc Bakery(store chan string) { for i := 1; i <= 10; i++ { チャンネルに値を渡す breadName := "bread " + strconv.Itoa(i) fmt.Println(breadName + "shipped to store!") store <- breadName チャンネルを閉じる } close(store)}func Consumer(store chan string) { for { bread, ok := <-store チャンネルが開いてい if !ok { break る限り値を取得 } fmt.Println("baught " + bread) }}func main() { store := make(chan string) fmt.Println("store open!") 並列化 go Bakery(store) go Consumer(store) http://play.golang.org/p/6RhknPqi2d time.Sleep(60 * time.Second)} 30
31.
言語仕様 channel 非同期な処理同士のやり取りを行うための窓口素数計算// A concurrent prime sieve // The prime sieve: Daisy-chain Filter processes. func main() {package main ch := make(chan int) // Create a new channel. go Generate(ch) // Launch Generate goroutine.// Send the sequence 2, 3, 4, ... to channel ch. for i := 0; i < 10; i++ {func Generate(ch chan<- int) { prime := <-ch for i := 2; ; i++ { print(prime, "n") ch <- i // Send i to channel ch. ch1 := make(chan int) } go Filter(ch, ch1, prime)} ch = ch1 }// Copy the values from channel in to channel out, }// removing those divisible by prime.func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from in. if i%prime != 0 { out <- i // Send i to out. } }} http://play.golang.org/p/9U22NfrXeq 31
35.
標準パッケージ 便利な標準ツール群Goでの開発を助ける標準ツール• go build パッケージをビルド• go get 必要なパッケージを取得• go install 必要なパッケージを取得&ビルド• go run 一時的にビルドし実行• go test テストとベンチマークを実行• gofmt フォーマットを直してくれる• godoc ドキュメントを生成 35
36.
標準パッケージ 便利な標準ツール群 go test • xxx_test.go内のTestXxxやBenchmarkXxxとい う関数を実行するコード例 結果package main % go testimport ( PASS "testing" ok _/…/main 0.006s)func TestAdd(t *testing.T) { const n, m = 2, 3 const want = 6 if out := Add(n, m); out != want { t.Errorf(”%v, want %v", out, want) }} 36
37.
標準パッケージ 便利な標準ツール群gofmt Bike Shed• 「インデント幅は4にしろ!」「いや8だ!」• 「ifと{の間にはスペース開けるだろ!」• 「演算子の前後にスペースは開けるだろ!」コミット前に“gofmt -w”と打てばいいだけ自動化スクリプト等が同梱されているので設定しましょう 37
44.
外部サービス Travis CI• 有名なCIサービス• GitHub上のレポジトリを継続テスト• go testを走らせるだけ.travis.yaml の例language: goscript: go test http://about.travis-ci.org/docs/user/languages/go/ 44
45.
外部サービス drone.io• CIサービスとしてTravis CIの最右翼• GitHub, Bitbucket, Google Codeが利用可 – Git, Mercurial, Bazaar, Subversionを利用可• go testを走らせるだけBuild Commandsの例go getgo buildgo test -short 45
52.
情報 チュートリアルたいていのことは公式サイトに載っている• A Tour of Go – http://tour.golang.org/• パッケージ一覧 – http://golang.org/pkg/• Go公式サイト – http://golang.org/doc/• 日本語訳サイト – http://golang.jp/ 52
たしかにー。最初違う例文書いててそのまま書いちゃったのがまずかったですね
http://play.golang.org/p/n_IoYSlsDn