Go Arrays:
Arrays in Go are fixed-size collections of elements of the same type. Here's a complete guide to understanding and using arrays in Go.
Declaring Arrays
You can declare an array with a specific length and type like this:
go
var numbers [5]int
fmt.Println(numbers)Initializing Arrays
You can initialize arrays during declaration:
go
var primes = [5]int{2, 3, 5, 7, 11}
fmt.Println(primes)Accessing and Modifying Elements
Use index-based access to get or set elements:
go
primes[0] = 17
fmt.Println(primes[0])Array Length
Use the built-in len function to get the length of an array:
go
fmt.Println(len(primes))Iterating Over Arrays
You can iterate over arrays using a for loop or range:
go
for i, value := range primes {
fmt.Println(i, value)
}Multidimensional Arrays
Arrays can have multiple dimensions:
go
var matrix [2][2]int = [2][2]int{{1, 2}, {3, 4}}
fmt.Println(matrix)