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 zeroF64 float64
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.Println("zero values for basic types:")
fmt.Printf("bool:       %v\n", zeroBool)
fmt.Printf("int:        %v\n", zeroInt)
fmt.Printf("float32:    %v\n", zeroF32)
fmt.Printf("float64:    %v\n", zeroF64)
fmt.Printf("string:     %#v\n", zeroStr)
fmt.Printf("pointer:    %v\n", zeroPtr)
fmt.Printf("slice:      %v\n", zeroSlice)
fmt.Printf("map:        %#v\n", zeroMap)
fmt.Printf("interface:  %v\n", zeroInterface)
fmt.Printf("channel:    %v\n", zeroChan)
fmt.Printf("array:      %v\n", zeroArray)
fmt.Printf("struct:     %#v\n", zeroStruct)
fmt.Printf("function:   %v\n", zeroFunc)
zero values for basic types:
bool:       false
int:        0
float32:    0
float64:    0
string:     ""
pointer:    <nil>
slice:      []
map:        map[string]int(nil)
interface:  <nil>
channel:    <nil>
array:      [0 0 0 0 0]
struct:     main.struc{a:0, b:""}
function:   <nil>

  ↑ ↓ to navigate     ↵ to select     Esc to close