Go json-rpc 返回空响应。

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

Go json-rpc returns empty respond

问题

我正在使用Go标准库编写一个使用JSON-RPC协议的客户端-服务器交互。服务器端运行正常,但我无法获得正确的响应,我得到的是空响应。响应类型是公共的。

服务器端:

func (h *Handler) AddUser(request UserAuthRequest, respond *uuid.UUID) error {
    log.Printf("AddUser: request:%+v", request)
    id := uuid.New()
    respond = &id
    log.Printf("%v", respond)  // 打印生成的ID
    return nil
}

客户端:

var x uuid.UUID
err = client.Call(
    "Handler.AddUser",
    &UserAuthRequest{},
    &x,
)
if err != nil {
    log.Fatalf("bad request: %v", err)
}
log.Printf("res: %+v", x)  // 打印 00000000-0000-0000-0000-000000000000

如何获得非空的响应?

英文:

I'm writing a client-server interaction using json-rpc protocol, via Go standard library.
Server side works fine, but i can't get correct respond, i get empty respond.
Respond type is public.

Server side

func (h *Handler) AddUser(request UserAuthRequest, respond *uuid.UUID) error {
	log.Printf("AddUser: request:%+v", request)
	id := uuid.New()
	respond = &id
	log.Printf("%v", respond)  // prints generated ID
	return nil
}

Client side

var x uuid.UUID
err = client.Call(
	"Handler.AddUser",
	&UserAuthRequest{},
	&x,
)
if err != nil {
	log.Fatalf("bad request: %v", err)
}
log.Printf("res: %+v", x)  // prints 00000000-0000-0000-0000-000000000000

How can i get non-empty respond

答案1

得分: 0

嗯,解决方案非常明显。
不是将指针respond重新分配给&id,而是需要更改*respond的值。
所以只需将respond = &id这一行改为*respond = id
就这样了。

英文:

Well, the solution on quite obvious
Instead of reassign pointer respond to &id, i need to change value of *respond.
So just change line respond = &id to *respond = id
That's all

huangapple
  • 本文由 发表于 2022年5月25日 09:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/72371135.html
匿名

发表评论

匿名网友

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

确定