字符串的0是什么?

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

转载于:https://stackoverflow.com/questions/12703243/what-is-the-zero-for-string

That's "" :

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

A string cannot be nil (but a *string can).

You can simply test

if stringId=="" {

To pass a zero string in stringID, use

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.

var str string
fmt.Println(str == "")

put above into your script, and go run it.

you can get the true, golang's string default value is "".

in this case empty string, or you can use NewIncompleteKey()