Go · Stage 5 – Grouping Data · Lesson 32 of 64

Adding to a Slice with append

Grow a slice and keep the returned slice value.

Go code

names := []string{"Maya"}
names = append(names, "Noah")
fmt.Println(names)
Line-by-line explanation
  1. A one-item slice is created.
  2. Append receives the slice and new value.
  3. The returned slice is assigned back to names.
  4. Both names are displayed.
Expected output
[Maya Noah]

This is the expected result.