golang:什么是字符串的零?

前端之家收集整理的这篇文章主要介绍了golang:什么是字符串的零?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
func NewKey(c appengine.Context,kind,stringID string,intID int64,parent *Key) *Key

文档说:

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.

字符串的零是什么?

我试过0和nil,我得到错误像:

cannot use nil as type string in function argument
那是 ”” :
var s string
fmt.Println(s=="") // prints "true"

字符串不能为nil(但是*字符串可以)。

你可以简单测试

if stringId=="" {

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

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

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.

猜你在找的Go相关文章