Slices
8 min · Go
Go's resizable lists: add items and reach them by index.
Enemy 1 of 5
What a slice is
This code makes a slice of three fruits and prints the first one, since counting starts at 0. Press Run to see it.
Why this works
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.