Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array: empty and fill

On the other hand we could also initialize an array with only the size, without initial values. In this case the default values in the array will be the 0 values of the appropriate type.

package main

import (
	"fmt"
)

func main() {
	var res [3]int

	fmt.Println(res)      // [0 0 0]
	fmt.Println(res[1])   // 0
	fmt.Println(len(res)) // 3

	fmt.Printf("%T\n", res) // [3]int

	res[1] = 85
	res[0] = 97
	res[2] = 93

	fmt.Println(res)      // [97 85 93]
	fmt.Println(res[1])   // 85
	fmt.Println(len(res)) // 3

	fmt.Printf("%T\n", res) // [3]int
}