Arrays

The type [n]T is an array of n values of type T.

The expression

  1. var a [10]int

declares a variable a as an array of ten integers.

An array's length is part of its type, so arrays cannot be resized. This seems limiting, but don't worry; Go provides a convenient way of working with arrays.

array.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var a [2]string
  5. a[0] = "Hello"
  6. a[1] = "World"
  7. fmt.Println(a[0], a[1])
  8. fmt.Println(a)
  9. primes := [6]int{2, 3, 5, 7, 11, 13}
  10. fmt.Println(primes)
  11. }