Upload
takuya-ueda
View
2.806
Download
0
Embed Size (px)
Citation preview
イケてるGopherになれる5つのポイント
2013/06/17(火)@ヒカルのgo
ポイント 1channelとgoroutineを使いこなせ!!
http://www.slideshare.net/takuyaueda967/goroutinechannelgogolang2
for - selectパターン
● 各Goroutineが無限ループになっており、イベントリスナー的にChannelを使うパターン
Goroutine-1 Channel-1
Channel-2sele
ct
for{}
Goroutine-2for{}
Goroutine-3for{}
Gopher君で表すとこんな感じ!
ひたすら本を入れる
ひたすら本を運ぶ
ひたすら台車を運ぶ
ひたすら本を燃やす
ポイント 2structの埋め込みを使いこなせ!!
http://qiita.com/tenntenn/items/e04441a40aeb9c31dbafhttp://qiita.com/tenntenn/items/f2a154dd436c0fa37c5b
埋め込み
type Hoge struct { N int}
type Piyo struct { Hoge M int}
func main() { piyo := &Piyo{Hoge{1}, 2} fmt.Println(piyo.N, piyo.M) fmt.Println(piyo.Hoge.N, piyo.M)}
埋め込みを使ったインタフェースの実装
type Hoge interface {A()B()
}
type Fuga struct{ *Piyo }func (f *Fuga) A() { fmt.Println("Fuga A") }
type Piyo struct{}func (p *Piyo) B() { fmt.Println("Piyo B") }
func main() {var hoge Hoge = &Fuga{&Piyo{}}hoge.A()hoge.B()
}
ポイント 3typeをうまく使いこなせ!!
http://qiita.com/tenntenn/items/c3afc87a20d9f50998bb
組込み型のエイリアス型を作る
type Hex intfunc (h Hex) String() string { return fmt.Sprintf(“0x%x”, int(h))}
関数に実装させる
type TaskFunc func()
func (f TaskFunc) Do() {f()
}
net/httpでも使われている
http.HandlerFunc
ポイント 4reflectをうまく使いこなせ!!
http://www.slideshare.net/takuyaueda967/reflect-27186813https://github.com/goken/goken/blob/master/goken08-reflect/goken08-reflect.md
標準ライブラリでも結構使われている
● encoding/jsonとかで使われている● ジェネリクスがないので、reflectを使う場面が多
い
func Unmarshal(data []byte, v interface{}) error
interface{}型でポインタを受け取り、reflectパッケージで値を設定している
ポイント 5Gopher君をうまく使いこなせ!!
https://github.com/golang-samples/gopher-3dhttps://github.com/golang-samples/gopher-vector
シーン:チケットの起票
(( [チケット] [チケット] ʕ ◔ϖ◔ʔ [チケット] )) |ヽ○==○ [チケット] c | ||_ | [チケット] し' ̄(_)) ̄(_)) ̄(_)
シーン:進捗を煽る
バン はよバン ʕ∩ ◔ϖ◔ʔ バン はよ / ミつ/ ̄ ̄ ̄/  ̄ ̄\/___/
シーン:ランチに誘う
バン はら減ったバン ʕ∩ ◔ϖ◔ʔ バン はら減った / ミつ/ ̄ ̄ ̄/  ̄ ̄\/___/
シーン:3Dゲームを作る
シーン:合体させてみる
まとめ
● channelとgoroutineを使いこなす● structの埋め込みを使いこなす● typeをうまく使いこなす● reflectをうまく使いこなす● Gopher君をうまく使いこなす
Go言語の初心者が見ると幸せになれる場所http://qiita.com/tenntenn/items/0e33a4959250d1a55045
時間あまったら
codegangsta/cli 便利!
https://github.com/codegangsta/cli● コマンドラインツールを作るためのライブラリ● コマンドライン引数が簡単に扱える● サブコマンドが簡単に作れる