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?

30th Jun 2021, 8:26 PM
Alex Aloshchenko
Alex Aloshchenko - avatar
2 odpowiedzi
+ 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]...)
30th Jun 2021, 10:32 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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]...).
1st Jul 2021, 5:51 AM
Alex Aloshchenko
Alex Aloshchenko - avatar