大猩猩 jsonrpc 得到空响应

Gorilla jsonrpc getting empty response

为什么我的 jsonrpc 方法 return 是一个空响应?

type Args struct {
  A, B int
}

type Response struct {
  sum int
  message string
}

type Arith int

func (t *Arith) Add(r *http.Request, args *Args, reply *Response) error {
  reply.sum = args.A + args.B
  reply.message = "Do math"

  // this does not work either

  //*reply = Response{
  //  sum : 12,
  //  message : "Do math",
  //}

  return nil
}

要求:

{"method":"Arith.Add","params":[{"A": 10, "B":2}], "id": 1}

回复:

{
  "result": {},
  "error": null,
  "id": 1
}

但是,如果我将 reply 的类型设置为 *string,那么这将正常工作:

*reply = "Responding with strings works"

回复:

{
  "result": "Responding with strings works",
  "error": null,
  "id": 1
}

我正在使用 http://www.gorillatoolkit.org/pkg/rpc

您的 Response 字段未导出。名称应大写:

type Response struct {
    Sum     int
    Message string
}