Thuta Learning
IntermediateProgrammingbeginner

Arrays

Relax. We'll talk through this in plain words — no textbook voice.

Array is a fixed-size collection. It stores values of the same type, indexed by position. In Go, the array's size is part of its type, so [3]int and [4]int are actually different types.

go
package main

import "fmt"

func main() {
    scores := [3]int{80, 90, 100}

    fmt.Println(scores)
    fmt.Println("first score:", scores[0])
    fmt.Println("total items:", len(scores))
}

scores[0] gets the first item. Since indexes in programming usually start at 0, the first item is at index 0.

You should see
[80 90 100] first score: 80 total items: 3

Info

Because arrays have a fixed size, real projects tend to reach for slices more often when the amount of data can change.

Easy traps

  • Accessing an out-of-range index, like scores[3], causes an error. Always check the size with len().
Arrays | Thuta Learning