きっかけ
JetBrainsさんがGo用のIDEを公開しはじめ、Go言語利用に乗り遅れがちな自分としては
この機会を逃す訳にはいきませんでした。結論は、「素晴らしいIDEである」の一言です。
語弊を招きますが、Goを理解しなくても作業が進められました。
やったこと
Goプロジェクトの作成
PubNubのインポート
bitFlyerさんの取引手数料を取得するリアルタイムAPIの試打
ソース
pubnub_go.go
package main
// PubNub試す参考URL
// https://www.pubnub.com/docs/go/data-streams-publish-and-subscribe
import (
"fmt"
"html"
"net/http"
// 記載するだけでExternal Librariesに勝手に追加・・・
"github.com/pubnub/go/messaging"
"encoding/json"
)
func handler2(w http.ResponseWriter, r *http.Request) {
// PubNubのバージョンを表示
fmt.Fprintf(w, "PubNub SDK for go;, %q", messaging.VersionInfo())
}
// サンプルべた書き
func main() {
http.HandleFunc("/hello", handler2)
pubnubTest := messaging.NewPubnub("demo", "sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f", "", "", false, "", nil)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubTest.Subscribe("lightning_ticker_BTC_JPY", "", successChannel, false, errorChannel)
for {
select {
case response := <-successChannel:
var msg []interface{}
err := json.Unmarshal(response, &msg)
if err != nil {
fmt.Println(err)
return
}
switch m := msg[0].(type) {
case float64:
fmt.Println(msg[1].(string))
case []interface{}:
fmt.Printf("Received message '%s' on channel '%s'\n", m[0], msg[2])
return
default:
panic(fmt.Sprintf("Unknown type: %T", m))
}
case err := <-errorChannel:
fmt.Println(string(err))
case <-messaging.SubscribeTimeout():
fmt.Println("Subscribe() timeout")
}
}
http.ListenAndServe(":8080", nil)
}
やってみた感想
Goの敷居がぐっと下がった様に感じます。
ただ、下がりすぎて詳細を追えていません・・・
夏休みの宿題にします。必ずやります。