go-tour/append.go

26 lines
408 B
Go
Raw Permalink Normal View History

2020-02-05 03:44:36 +00:00
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)
}