0
how to make valid parameter to the variadic function
I have made the following variadic function for the counter task: func count (num int, data ...int, ch chan int) { var counter int for _, v := range data { switch v { case num : counter++ } } ch <- counter } ... go count(num, data[:len(data)/2], ch1) ... Why the second parameter is not accepted?
2 Respuestas
+ 3
The variadic parameter needs to be the last parameter in the func, otherwise the number of required arguments is ambiguous. Try this instead;
func (num int, ch chan int, data ...int){...code...}
Then you also need to spread the arguments like;
go count(num, ch1, data[:len(data)/2]...)
+ 1
Thank you so much for the answer. I never would have guessed about putting ellipsis here: go count(num, ch1, data[:len(data)/2]...).