解包可变参数


对于你的例子,


package main


import "fmt"


func run(a, b string, list ...int) {

    fmt.Println(list)

    for i, e := range list {

        fmt.Println(i, e)

    }

}


func call(list ...int) {

    fmt.Println(list)

    var x, y string

    run(x, y, list...) // use "list..." is allowed

}


func main() {

    a := []int{1, 2, 3}

    fmt.Println(a)

    call(a...)

}

游乐场: https: //play.golang.org/p/NI0L-nVVUxm


输出:


[1 2 3]

[1 2 3]

[1 2 3]

0 1

1 2

2 3