- append
Slice append
examples/slice-append/slice_append.go
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
- Both the actual length and the capacity grew