字符串的零是什么?

huangapple go评论77阅读模式
英文:

What is the zero for string?

问题

func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key

文档中说:

> NewKey 创建一个新的键。kind 不能为空。stringID 和 intID 必须至少有一个为 。如果两者都为零,则返回的键是不完整的。parent 必须是一个完整的键或者为 nil。

string 的零是什么?

我尝试了 0nil,但是我得到了如下错误:

无法将 nil 用作函数参数中的 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

答案1

得分: 231

这是""

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

一个字符串不能为nil(但*string可以)。

你可以简单地测试:

if stringId=="" {

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

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

来自规范

> 当为一个值分配内存时,无论是通过声明还是通过make或new的调用,如果没有提供显式的初始化,内存都会被赋予默认初始化。这样一个值的每个元素都被设置为其类型的零值:布尔值为false,整数为0,浮点数为0.0,字符串为"",指针、函数、接口、切片、通道和映射为nil。

英文:

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

得分: 0

在这种情况下,可以使用空字符串,或者您可以使用NewIncompleteKey()。

英文:

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

huangapple
  • 本文由 发表于 2012年10月3日 14:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/12703243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定