Arrays
An array is a numbered sequence of elements with a length fixed at compile time:
package main
import "fmt"
func main() {
var a [5]int // five ints, all zero
a[0] = 100
fmt.Println(a) // [100 0 0 0 0]
primes := [5]int{2, 3, 5, 7, 11}
fmt.Println(primes[4], len(primes)) // 11 5
}
var a [5]int gives you five ints already initialized to 0 — Go's
zero-value rule applies to every element, so there's no uninitialized memory
to worry about. Index with a[i], get the length with len(a).
The length is part of the type
Here is the surprising bit: [5]int and [6]int are different types.
var five [5]int
var six [6]int
// five = six // compile error: cannot use [6]int as [5]int
A function that takes [5]int accepts arrays of exactly five ints and
nothing else. You can't grow an array, and you can't pass a [10]int where a
[5]int is expected. The length is baked in, the same way int and string
are different types.
Arrays are values
In Go, an array is a value like any other. Assigning one array to another — or passing it to a function — copies every element:
a := [3]int{1, 2, 3}
b := a // b is a full copy
b[0] = 99
fmt.Println(a, b) // [1 2 3] [99 2 3]
a is untouched. This is Go's value semantics at work: what you have is
yours, and no one else can change it behind your back. Many languages make
arrays references silently; Go makes copying explicit and predictable. The
cost is equally predictable — copying a [1_000_000]int into a function
copies a million ints.
So why does anyone use slices?
Fixed length plus copy-on-assign makes arrays great for a few niches — hashes, fixed-size buffers, lookup tables — and awkward for everyday work. Most real code needs "a list of things whose length I don't know yet", and for that Go provides the slice: a lightweight, growable view over an array.
Arrays still matter, though, because slices are built on top of them. Every slice you'll ever use has an array quietly sitting underneath it. Understanding that arrays are fixed-size values is the key to understanding everything slices do in the next lesson.
For now, practice the array itself: the exercise asks you to sum a [5]int.
Notice that the signature guarantees you exactly five elements — no empty
case to handle. That guarantee is the fixed length working for you.
Practice
1. Sum an Array
BeginnerImplement
sumArrayso it returns the sum of all five elements:func sumArray(nums [5]int) intFor example,
sumArray([5]int{1, 2, 3, 4, 5})returns15, andsumArray([5]int{})returns0(five zero-valued ints).Note the parameter type:
[5]int, an array, not a slice. The length is part of the type, so your function is guaranteed exactly five elements — there is no empty case. Loop over them (either a three-partforwithlen(nums), orfor _, n := range nums) and accumulate the total.package main import "fmt" // sumArray returns the sum of all five elements of nums. func sumArray(nums [5]int) int { // TODO: implement return 0 } func main() { fmt.Println(sumArray([5]int{1, 2, 3, 4, 5})) }Sign in to write and run your solution in the browser.
Sign in for hints and the solution.