go / beginner
Snippet
Basic Arrays
Arrays in Go have a fixed length that is part of their type. Once defined, their size cannot change.
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
package mainimport "fmt"func main() {var ids [3]intids[0] = 101ids[1] = 102ids[2] = 103names := [2]string{"Alice", "Bob"}fmt.Println(ids, names)}
Breakdown
1
var ids [3]int
Declares an array that can hold exactly 3 integers.
2
ids[0] = 101
Assigns a value to the first index (zero-based).
3
[2]string{"Alice", "Bob"}
Short-hand syntax to declare and initialize an array simultaneously.