append
The built-in Go function that adds values to the end of a slice and returns the updated slice.
In more detail
Always assign the result back, as in items = append(items, "scheduler"), because append may move the data to new, larger storage. Appending to a nil slice works, so there is no need to create an empty one first.
Example
names = append(names, "api", "worker")Related terms
- slice — A flexible, growable view over a list of values of one type, written like []string, and Go's everyday list.
- len — The built-in Go function that returns how many items a string, slice, map, array or channel holds.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Arrays, Slices, and Maps — Concurrent Systems Programming with Go