All lessons

Slices

8 min · Go

Go's resizable lists — add items and reach them by index.

Go · Slices0/5 cleared

Enemy 1 of 5

What a slice is

Go does have fixed-size arrays, but everyday Go code almost always reaches for a slice instead — a flexible, resizable view over a sequence of values that can grow as your program runs. []string{"apple", "banana", "cherry"} creates a slice of strings with those three starting values already in it.

Just as in every language you've seen so far, indexing starts at 0: fruits[0] is the first element, fruits[1] the second, and so on. Reading past the end (an index equal to or greater than the slice's length) causes a runtime panic — Go's term for a crash triggered by an invalid operation like this — rather than silently returning garbage.

You
Invisible Heisenbug

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: apple