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

Slice append

  • append
package main

import (
	"fmt"
)

func main() {
	dwarfs := []string{}
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))

	dwarfs = append(dwarfs, "Happy")
	d := dwarfs
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))
	fmt.Println()

	dwarfs = append(dwarfs, "Grumpy", "Sleepy", "Doc", "Bashful", "Sneezy", "Dopey")
	fmt.Println(d)
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))
	fmt.Println()

	dwarfs = append(dwarfs, "Snow white")
	fmt.Println(d)
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))
}
[]
0
0
[Happy]
1
1

[Happy]
[Happy Grumpy Sleepy Doc Bashful Sneezy Dopey]
7
7

[Happy]
[Happy Grumpy Sleepy Doc Bashful Sneezy Dopey Snow white]
8
14