What is the zero for string?

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.

What is the zero for string?

I tried 0 and nil, and I got errors like:

cannot use nil as type string in function argument
101314 次浏览

这就是 "":

var s string
fmt.Println(s=="") // prints "true"

字符串不能为零(但 *string可以为零)。

您可以简单地测试

if stringId=="" {

若要在 stringID中传递零字符串,请使用

k := NewKey(c, "kind", "", 0, p)

来自 说明书:

当分配内存以存储值时,可以通过 声明或者 make 或 new 调用,并且没有显式的初始化 如果提供了默认初始化,则会给内存一个默认初始化 元素的值设置为其类型的零值: false 布尔值为0,整数为0,浮点数为0.0,“绳子”和零 用于指针、函数、接口、切片、通道和映射。

在本例中为空字符串,或者可以使用 NewInCompleteKey ()