Golang ってネットワークを扱うのが凄く楽で色んな物が作りたくなるんだけど、いつも pubsub っぽいのが欲しくなって毎回作ってる気がしたので汎用的に扱えるインタフェースを作った。 mattn/go-pubsub - GitHub https://github.com/mattn/go-pubsub 使い方は簡単で、まず subscribe 側はある型を引数に持つコールバックで Sub を呼び出す。 ps := pubsub.New() ps.Sub(func(i int) { fmt.Println("int subscriber: ", i) }) そして publish 側はある型を指定して Pub を呼び出す。 ps.Pub(1) Pub された値と同じ型の引数を持つ subscriber のみがメッセージを受け取れるという仕組み。構造体も渡せるので複雑なメッセージ内容もOK