旧行李 2012-10-03 06:48 采纳率: 25%
浏览 789
已采纳

字符串的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

  • 写回答

3条回答 默认 最新

  • lrony* 2012-10-03 06:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?