go-tour/append.go
2020-02-04 21:44:36 -06:00

26 lines
408 B
Go

package main
import "fmt"
func main() {
var s []int
printSlice(s)
// append works on nil slices
s = append(s, 0)
printSlice(s)
// the slice grows as needed
s = append(s, 1)
printSlice(s)
// we can add more than one element at a time
s = append(s, 2, 3, 4)
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}