Go: Default value of struct, string, slice, map

Variables declared without an explicit initial value are set to their zero values:

  • 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; for example each element of an array of structs will have its fields zeroed if no value is specified.

var n int // n == 0
type T struct {
	n int
	f float64
	next *T
t := new(T) // t.n == 0, t.f == 0.0 and t.next == nil


Be the first to comment!