Go append slice

package main

import "fmt"

func main() {
    var s []int
    printSlice(s)

    s = append(s, 1)
    printSlice(s)

    s = append(s, 2, 3, 4)
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("Length=%d, capability=%d, slice=%v\n", len(s), cap(s), s)
}
Length=0, capability=0, slice=[]
Length=1, capability=1, slice=[1]
Length=4, capability=4, slice=[1 2 3 4]

你可能感兴趣的:(Go append slice)