Arrays in Go

Contents
Fixed length
Read and write by index
Loop without len bugs
When arrays are right

Fixed length

An array holds a fixed number of elements of one type, and the length is part of the type itself: [3]int and [4]int are different types. Declare one with var to get zero values, or with a literal to set the content at once.

package main import "fmt" func main() { var scores [3]int fmt.Println(scores) nums := [4]int{1, 2, 3, 4} fmt.Println(nums, len(nums)) }

go run main.go

[0 0 0]
[1 2 3 4] 4

A fresh array is never empty garbage: every element starts at the zero value of its type. The size in brackets is a constant, so it cannot come from a variable.

Read and write by index

Elements are numbered from zero, so a three element array uses indexes 0, 1, and 2. Read an element with a[i] and store a new one with a[i] followed by an assignment.

package main import "fmt" func main() { colors := [3]string{"red", "green", "blue"} colors[1] = "yellow" fmt.Println(colors[0], colors[1], colors[2]) }

red yellow blue

Going outside the valid range stops the program with a panic, which is the Go way of refusing to guess. That strictness turns off by one mistakes into loud failures instead of silent corruption.

Loop without len bugs

The classic indexing bug is walking past the last element. Loop with i starting at 0 while i stays below the result of len, and the bound always matches the array even after edits.

package main import "fmt" func main() { nums := [5]int{1, 2, 3, 4, 5} total := 0 for i := 0; i < len(nums); i++ { total += nums[i] } fmt.Println(total) }

15

Prefer len over a hardcoded number: if the literal grows from five elements to six, the loop follows automatically. Range loops from the previous lesson remove the index math completely.

When arrays are right

Arrays are values: assigning one variable to another copies every element. That makes small fixed size collections safe to pass around, for example coordinates, color triples, or a week of totals.

package main import "fmt" func main() { a := [2]int{1, 2} b := a b[0] = 9 fmt.Println(a, b) }

[1 2] [9 2]

For anything that grows, slices from the next lesson are the better tool. Reach for an array when the size is known up front and never changes.

Next: Slices

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: