如何从 RPC 调用中返回一个空字符串?

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

How do I return an empty string from an RPC call?

问题

我有一个RPC系统,其中用于结果的接口如下所示:

type ValReply struct {
    Val string
}

有时,我的RPC会将reply.Val设置为""(空字符串)。在这些情况下,reply.Val中的先前值不会被覆盖,导致客户端使用错误的结果。

我该如何使我的RPC调用返回一个空字符串?

我已经搜索了这个问题,但在RPC API中没有找到关于不返回空字符串的任何信息。

英文:

I have an RPC system, where the interface used for the result is as follows:

type ValReply struct {
	Val string 
}

Sometimes, my RPC will set reply.Val to ""(the empty string). In these cases, the previous value in reply.Val is not overwritten, leaving the incorrect result to be used by the client.

How can I get my RPC call to return an empty string?

I have googled this issue, and I can't find anything about not returning empty strings in the RPC API.

答案1

得分: 1

“”(空字符串)经常被库(数据库、rpc、json)解释为默认值并被忽略。

为了更好地控制nil和"",可以将rpc签名更改为:

type ValReply struct {
    Val *string 
}

然后库将区分空字符串和null等。

英文:

"" (the empty string) is often interpreted by libraries (database, rpc, json) as the default value and just ignored.

To get more control over nil, and "", change the rpc signature to:

type ValReply struct {
    Val *string 
}

and then the library will distinguish empty, from null etc.

huangapple
  • 本文由 发表于 2016年2月13日 08:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/35374389.html
匿名

发表评论

匿名网友

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

确定