Zero values

suggest change

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:

type zero value
bool false
integers 0
floating point numbers 0.0
string ""
pointer nil
slice nil
map nil
interface nil
channel nil
array all elements have zero value
struct all members set to zero value
function nil

Said differently:

 fmt.Println("zero values for basic types:")

var zeroBool bool
fmt.Printf("bool:       %v\n", zeroBool)

var zeroInt int
fmt.Printf("int:        %v\n", zeroInt)

var zeroF32 float32
fmt.Printf("float32:    %v\n", zeroF32)

var zeroF64 float64
fmt.Printf("float64:    %v\n", zeroF64)

var zeroStr string
fmt.Printf("string:     %#v\n", zeroStr)

var zeroPtr *int
fmt.Printf("pointer:    %v\n", zeroPtr)

var zeroSlice []uint32
fmt.Printf("slice:      %v\n", zeroSlice)

var zeroMap map[string]int
fmt.Printf("map:        %#v\n", zeroMap)

var zeroInterface interface{}
fmt.Printf("interface:  %v\n", zeroInterface)

var zeroChan chan bool
fmt.Printf("channel:    %v\n", zeroChan)

var zeroArray [5]int
fmt.Printf("array:      %v\n", zeroArray)

type struc struct {
	a int
	b string
var zeroStruct struc
fmt.Printf("struct:     %#v\n", zeroStruct)

var zeroFunc func(bool)
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>

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents