Essential Go Basic types  Suggest an edit

Zero values

Variables in Go are initialized with a known value if not explicitly assigned.

That value is knows as zero value.

This is different from C/C++, where variables that are not explicitly assigned have undefined values.

The values of zero type are unsurprising:

typezero value
boolfalse
integers0
floating point numbers0.0
string""
pointernil
slicenil
mapnil
interfacenil
channelnil
arrayall elements have zero value
structall members set to zero value
functionnil

Said differently:

var zeroBool bool
var zeroInt int
var zeroF32 float32
var zeroStr string
var zeroPtr *int
var zeroSlice []uint32
var zeroMap map[string]int
var zeroInterface interface{}
var zeroChan chan bool
var zeroArray [5]int
type struc struct {
	a int
	b string
}
var zeroStruct struc
var zeroFunc func(bool)

fmt.Printf("zero bool:       %v\n", zeroBool)
fmt.Printf("zero int:        %v\n", zeroInt)
fmt.Printf("zero f32:        %v\n", zeroF32)
fmt.Printf("zero string:     %#v\n", zeroStr)
fmt.Printf("zero ptr:        %v\n", zeroPtr)
fmt.Printf("zero slice:      %v\n", zeroSlice)
fmt.Printf("zero map:        %#v\n", zeroMap)
fmt.Printf("zero interface:  %v\n", zeroInterface)
fmt.Printf("zero channel:    %v\n", zeroChan)
fmt.Printf("zero array:      %v\n", zeroArray)
fmt.Printf("zero struct:     %#v\n", zeroStruct)
fmt.Printf("zero function:   %v\n", zeroFunc)
zero bool:       false
zero int:        0
zero f32:        0
zero string:     ""
zero ptr:        <nil>
zero slice:      []
zero map:        map[string]int(nil)
zero interface:  <nil>
zero channel:    <nil>
zero array:      [0 0 0 0 0]
zero struct:     main.struc{a:0, b:""}
zero function:   <nil>

  ↑ ↓ to navigate     ↵ to select     Esc to close