英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论