is coming soon...
ado.xyz

Default Values for Go Types

May 5, 2020

The Go programming language has a plethora of primitive types. In this post, we’ll learn what their default or zero values are. Understanding the zero value of a type is important as it will impact how you work with it.

Go for the most part has very sensible default values. There are some gotchas to be aware of and I’ll call them out, but let’s dive right in. We’ll start with the basic building blocks and work our way up to more advanced types.

Default Value for Strings in Go

The default or zero value for strings in the Go programming language is an empty string (i.e "").

package main

import "fmt"

func main() {
  var message string

  fmt.Println(message) // ""
}

Default Value for Integers in Go

The Go programming language has a number of primitive integer types. They are:

  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • uintptr
  • byte - alias for uint8
  • rune - alias for int32, represents a Unicode code point

The default or zero value for all of these integers types in the Go programming language is the whole number 0.

package main

import "fmt"

func main() {
   var num int
   var num1 int8
   var num2 uint16
   var num3 uintptr
   var num4 byte
   var num5 rune

   fmt.Println(num, num1, num2, num3) // 0, 0, 0, 0, 0, 0
}

Default Value for Floats in Go

Just like with integers, the Go programming language has multiple types to represent floats, although that number is much smaller. Types of floats in Go consist of float32 and float64. The default value for floats in Go is 0, the same as integers.

package main

import "fmt"

func main() {
   var num int32
   var num1 int64

   fmt.Println(num, num1) // 0, 0, 0, 0
}

Default Value for Complex Numbers in Go

The Go programming language also has two primitive types for representing complex numbers that may or may not have imaginary parts. The two types are complex64 and complex128 and their default or zero value is (0+0i).

package main

import "fmt"

func main() {
   var num complex64
   var num1 complex128

   fmt.Println(num, num1) // (0+0i), (0+0i)
}

Default Value for Booleans in Go

The default value for the bool type in the Go programming language is false.

package main

import "fmt"

func main() {
   var facts bool

   fmt.Println(facts) // false
}

Default Value of Pointers in Go

A pointer holds the memory address of a value. The zero value of a pointer in the Go programming language is nil.

package main

import "fmt"

func main() {
   var num *int

   fmt.Println(num) // nil
}

Default Value for Structs in Go

Structs are collections of primitive types or even other structs and they inherit the zero values of the types they are made out of. Let’s see an example of this:

package main

import "fmt"

type Person struct {
  Name string
  Age int
}

func main() {
  var ado Person

  fmt.Println(ado) // { "", 0 }
}

Default Value of Slices in Go

Unlike arrays, the zero value for slices in the Go programming language nil. A nil slice has a length and capacity of 0 and has no underlying array.

Note: If you print out an empty slice, you’ll get back a “[]” response instead of a nil, but if you check if an empty slice equals nil, you’ll get back a response of true.

package main

import "fmt"

func main() {
	var s []int
	fmt.Println(s) // []
	if s == nil { // true
		fmt.Println("nil!")
	}
}

Default Value of Maps in Go

The zero value of a map in Go is nil. Similar to a slice, if you just print out the value of an empty map, you’ll get a response of map[], but if you check if the map equals nil, you’ll get back a response of true.

package main

import "fmt"

func main() {
  var data map[string]interface{}
  fmt.Println(data) // map[]
  if m == nil { // true
  fmt.Println("nil!")
  }
}

Default Value of an Empty Interface in Go

Finally, the zero value of an empty interface (i.e. interface{}) in Go is also nil. An empty interface can hold values of any type and you’re likely to come across empty interfaces from time to time when building applications in Go.

package main

import "fmt"

func main() {
  var data interface{}
  fmt.Println(data) // nil
}

The Go programming language has sane and very straightforward default or zero values for its many primitive types. It is very important to understand what the zero value of a type is as it can impact how you work with it.

Newsletter

Subscribe to the newsletter and be the first to know when I publish new content. No Spam.