美文网首页Go
Go append slice

Go append slice

作者: JaedenKil | 来源:发表于2019-03-08 15:02 被阅读0次
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

    本文链接:https://www.haomeiwen.com/subject/miprpqtx.html