-4
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.

4
  • The spec..
    – mkopriva
    Nov 10, 2017 at 14:12
  • I wanted to know how to read the arguments, I already know it supports variadic functions.
    – Daksh
    Nov 10, 2017 at 14:20
  • And the answer to that question - and many other places on SO and web search results for golang variadic functions - explain how to read the arguments.
    – Adrian
    Nov 10, 2017 at 14:23
  • Downvoted. Try searching on google first. There are numerous links explaining it.
    – Masquerade
    Jan 10, 2018 at 11:33

1 Answer 1

2

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 {
    // ...
}
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.