Calling the JSON-RPC service
This recipe will illustrate how procedures via the JSON-RPC protocol can be called with use of the standard library.
How to do it...
- Open the console and create the folder
chapter07/recipe11
. - Navigate to the directory.
- Create the
jsonrpc.go
file with the following content:
package main import ( "log" "net" "net/rpc" "net/rpc/jsonrpc" ) type Args struct { A, B int } type Result int type RpcServer struct{} func (t RpcServer) Add(args *Args, result *Result) error { log.Printf("Adding %d to %d\n", args.A, args.B) *result = Result(args.A + args.B) return nil } const addr = ":7070" func main() { go createServer(addr) client, err := jsonrpc.Dial("tcp", addr) if err != nil { panic(err) } defer client.Close() args := &Args{ ...