Initialize values for specific array elements in Go


When an array declare using an array literal, values can be initialize for specific elements.
A value of 10 is assigned to the second element (index 1) and a value of 30 is assigned to the fourth element (index 3).

Example

package main

import "fmt"

func main() {
	x := [5]int{1: 10, 3: 30}
	fmt.Println(x)
}

Output

[0 10 0 30 0]
Most Helpful This Week