According to the JSON-RPC 2.0 specification, JSON-RPC is a lightweight remote procedure call (RPC) protocol.
To install the package, run:
go get github.com/go-zoox/jsonrpc
// server
package main
import (
"context"
"github.com/go-zoox/jsonrpc"
"github.com/go-zoox/jsonrpc/server"
"github.com/go-zoox/logger"
)
func main() {
s := server.New()
s.Register("echo", func(ctx context.Context, params jsonrpc.Params) (jsonrpc.Result, error) {
logger.Info("params: %s", params)
return jsonrpc.Result{
"name": params.Get("name"),
"age": 18,
}, nil
})
s.Run()
}
// client
package main
import (
"context"
"github.com/go-zoox/core-utils/cast"
"github.com/go-zoox/jsonrpc"
"github.com/go-zoox/jsonrpc/client"
"github.com/go-zoox/logger"
)
func main() {
c := client.New("http://localhost:8080")
r, err := c.Call(context.Background(), "echo", jsonrpc.Params{
"name": "zero",
})
if err != nil {
logger.Errorf("failed to call: %s", err)
return
}
logger.Info("result: %d", cast.ToInt64(r.Get("age")))
}
GoZoox is released under the MIT License.