如何读取传递给采用某种可变参数的函数的所有参数? [重复]

This question already has an answer here:

func foo(x... int) {

//Do something with the arguments.

}

Function foo takes any number of arguments of a particular type. How do I read those arguments inside the function?

I can do so when a slice of int is passed to the function foo but not if arguments are not passed as a slice of int.

</div>

I can do so when a slice of int is passed to the function foo but not if arguments are not passed as a slice of int.

You can use "x" as a slice in both cases. The following should work regardless of the way you call the function:

for i, v := range x {
    // ...
}