大猩猩 jsonrpc 得到空响应。

huangapple go评论77阅读模式
英文:

Gorilla jsonrpc getting empty response

问题

为什么我的jsonrpc方法返回一个空响应?

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"
  
  // 这种方式也不起作用

  //*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。

英文:

Why does my jsonrpc method return an empty response?

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
}

Request:

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

Response:

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

However, if I set the type of reply to *string, then this will work fine:

*reply = "Responding with strings works"

Response:

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

I'm using http://www.gorillatoolkit.org/pkg/rpc.

答案1

得分: 3

你的Response字段是不可导出的。名称应该是大写的:

type Response struct {
    Sum     int
    Message string
}
英文:

Your Response fields are unexported. The names should be uppercase:

type Response struct {
    Sum     int
    Message string
}

huangapple
  • 本文由 发表于 2015年8月26日 18:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/32224644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定