What is the zero for string?

StringGo

String Problem Overview


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

String Solutions


Solution 1 - 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.

Solution 2 - String

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeanSinaeanView Question on Stackoverflow
Solution 1 - StringDenys SéguretView Answer on Stackoverflow
Solution 2 - StringNoypi GilasView Answer on Stackoverflow