HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

What is the zero for string?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
theforzerostringwhat

Problem

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

Solution

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.

Code Snippets

var s string
fmt.Println(s=="") // prints "true"
if stringId=="" {
k := NewKey(c, "kind", "", 0, p)

Context

Stack Overflow Q#12703243, score: 231

Revisions (0)

No revisions yet.