如何读取传递给采用某种类型的可变参数的函数的所有参数?

How do I read all the arguments passed to a function that takes a variadic parameter of some type?

func foo(x... int) {

//Do something with the arguments.

}

函数 foo 接受任意数量的特定类型的参数。我如何读取函数中的那些参数?

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.

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.

在这两种情况下,您都可以使用 "x" 作为切片。无论您调用函数的方式如何,以下内容都应该起作用:

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