Go standard library net/rpc/jsonrpc 不支援 HTTP

之前寫 JSON-RPC over HTTP 的時候是直接用 github.com/gorilla/rpc,蠻簡 單就寫出來。這次打算用 standard library 去做,不額外引用 gorilla,結果 踢到鐵板。

目前 net/rpc 支援兩種 codec,一種是 Go 內建的 encoding/gob,另一種 是 net/rpc/jsonrpc。gob 可以透過 HTTP 包起來使用,也可以獨立作為 TCP service。而 jsonrpc 則是「只能獨立使用」。這在 API 裡面並沒有說明得很清 楚。相關討論在: https://github.com/golang/go/issues/2738 ,裡面提到原因是

jsonrpc can be used with HTTP by writing a suitable codec. Given that there is no standard definition of jsonrpc over http, I think that’s the best we can do.

原來 jsonrpc over http 沒有標準定義啊…