capypad
0 day streak
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
go
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
 
import "fmt"
 
func main() {
var ids [3]int
ids[0] = 101
ids[1] = 102
ids[2] = 103
 
names := [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.