go / beginner
Snippet
Working with Slices
Slices are dynamic windows into arrays. Unlike arrays, slices can grow in size using the built-in 'append' function.
snippet.go
1
2
3
fruits := []string{"apple", "banana"}fruits = append(fruits, "cherry")fmt.Println(fruits[1]) // "banana"
Breakdown
1
fruits := []string{"apple", "banana"}
Creates a slice of strings with two initial elements.
2
append(fruits, "cherry")
Adds a new element to the slice and returns the updated slice.