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