47

Here is sample code:

package main

import (
    "fmt"
)

type A struct {
    Name string
}

func (this *A) demo(tag string) {
    fmt.Printf("%#v\n", this)
    fmt.Println(tag)
}

func main() {
    var ele A
    ele.demo("ele are called")

    ele2 := A{}
    ele2.demo("ele2 are called")
}

Run results:

&main.A{Name:""}
ele are called
&main.A{Name:""}
ele2 are called

It looks like those are the same about var ele A and ele2 := A{}

So, the struct's Zero value is not nil, but a struct that all of the property are initialized Zero value. Is the guess right?

If the guess is right, then the nature of var ele A and ele2 := A{} are the same right?

3 Answers 3

80

Why guess (correctly) when there's some documentation ?

When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value.

Each element of such a variable or 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.

This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.

Note that there's no way to set a struct value to nil (but you could set the value of a pointer to a struct to nil).

Sign up to request clarification or add additional context in comments.

1 Comment

@user98761 actually it does, though not very explicit and isolated but instead in the context of array initialization. But you can also infer this behavior of struct initialization with a bit of attention from the subsequently given examples.
18

I don't believe the top-voted answer is clearly worded to answer the question, so here is a more clear explanation:

The elements of an array or struct will have its fields zeroed if no value is specified. This initialization is done recursively:

type T struct {
    n int
    f float64
    next *T
}
fmt.Println([2]T{}) // [{0 0 <nil>} {0 0 <nil>}]

Source

Comments

2

Demonstrating @Denys Séguret's first-rate answer. Each element of such a variable or value is set to the zero value for its type (https://golang.org/ref/spec#The_zero_value):

package main

import "fmt"

func main() {
    // false for booleans,
    var bl bool // false
    //0 for numeric types,
    var in int // 0
    // "" for strings,
    var st string // ""
    // and nil for pointers, functions, interfaces, channels,
    var pi *int    // <nil>
    var ps *string // <nil>
    var fu func()      // <nil> Go vet error. https://stackoverflow.com/a/56663166/12817546
    var ir interface{} // <nil>
    var ch chan string // <nil>
    fmt.Println(bl, in, st, pi, ps, fu, ir, ch)
    // slices, and maps.
    var sl []int          // true
    var mp map[int]string // true
    var pm *map[int]string // <nil>
    fmt.Printf("%v %v %v\n", sl == nil, mp == nil, pm)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.