Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Append to a slice

package main

import "fmt"

func main() {
	base := [...]string{"a", "b", "c", "d", "e", "f", "g", "h"}
	fmt.Println(base)
	fmt.Println(len(base))
	part := base[3:7]
	fmt.Println(part)
	fmt.Println(len(part))
	fmt.Println(cap(part))
	fmt.Println()

	part = append(part, "X") // the slice was extended in the same array
	fmt.Println(part)
	fmt.Println(base)
	fmt.Println(len(part))
	fmt.Println(cap(part))
	fmt.Println()

	part = append(part, "Y") // creates a new, larger array and copyes the data.
	fmt.Println(part)
	fmt.Println(base)
	fmt.Println(len(part))
	fmt.Println(cap(part))
}
[a b c d e f g h]
8
[d e f g]
4
5

[d e f g X]
[a b c d e f g X]
5
5

[d e f g X Y]
[a b c d e f g X]
6
10