- make
Pre-allocate capacity for slice with make
examples/slice-pre-allocate/slice_pre_allocate.go
package main import ( "fmt" ) func main() { dwarfs := make([]string, 0, 10) fmt.Println(dwarfs) fmt.Println(len(dwarfs)) fmt.Println(cap(dwarfs)) dwarfs = append(dwarfs, "Happy") fmt.Println(dwarfs) fmt.Println(len(dwarfs)) fmt.Println(cap(dwarfs)) dwarfs = append(dwarfs, "Grumpy", "Sleepy") fmt.Println(dwarfs) fmt.Println(len(dwarfs)) fmt.Println(cap(dwarfs)) }
[] 0 10 [Happy] 1 10 [Happy Grumpy Sleepy] 3 10