Tsallaka zuwa abun ciki
Justice Tech School

Arrays

An array has a fixed length that is part of its type. Declare with [n]type. Use [...] to let the compiler count. Arrays are copied when assigned, so slices are used far more often.

Example
var a [3]int
a[0] = 10
b := [3]string{"x", "y", "z"}
c := [...]int{1, 2, 3, 4}
fmt.Println(a, b, len(c))
d := b
d[0] = "changed"
fmt.Println(b[0], d[0])

Result

[10 0 0] [x y z] 4
x changed

Exercise

Declare an array of 5 ints.

var a int

Quiz Test

Are arrays copied on assignment?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.