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
What is an Array in Go?
Declaring an Integer or String Array of Five Elements in Go
Golang check if array element exists
How to assign and access array element values in Go?
How to iterate over an Array using for loop?
Initializing an Array with ellipses in Go
Copy an array by value and reference into another array
How to initialize an Array with an Array Literal in Go?
Tricks to filter array elements in Go