string 字符串的零是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12703243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the zero for string?
提问by DeanSinaean
func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key
The documentation says :
文档说:
NewKey creates a new key. kind cannot be empty. Either one or both of stringID and intID must be zero. If both are zero, the key returned is incomplete. parent must either be a complete key or nil.
NewKey 创建一个新密钥。种类不能为空。stringID 和 intID 之一或两者必须为零。如果两者都为零,则返回的密钥不完整。parent 必须是完整的键或 nil。
What is the zero for string?
字符串的零是什么?
I tried 0
and nil
, and I got errors like:
我试过0
and nil
,我得到了如下错误:
cannot use nil as type string in function argument
回答by Denys Séguret
That's ""
:
那是""
:
var s string
fmt.Println(s=="") // prints "true"
A string cannot be nil (but a *string
can).
字符串不能为零(但*string
可以)。
You can simply test
你可以简单地测试
if stringId=="" {
To pass a zero string in stringID
, use
要在 中传递零字符串stringID
,请使用
k := NewKey(c, "kind", "", 0, p)
From the specification:
从规范:
When memory is allocated to store a value, either through a declaration or a call of make or new, and no explicit initialization is provided, the memory is given a default initialization. Each element of such a value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.
当通过声明或调用 make 或 new 分配内存来存储值时,并且未提供显式初始化,内存将被赋予默认初始化。这种值的每个元素都被设置为其类型的零值:布尔值为 false,整数为 0,浮点数为 0.0,字符串为"",指针、函数、接口、切片、通道和映射为 nil。
回答by Noypi Gilas
in this case empty string, or you can use NewIncompleteKey()
在这种情况下为空字符串,或者您可以使用 NewIncompleteKey()