+ 1

Hi y'all, I'm having trouble with the "concurrent counter" exercise regarding goroutines, what am I doing wrong?

Here my code package main import "fmt" //define the count() function go count(num int, data [] int, ch chan int){ var occurrance int for _, iter := range data{ if num == iter{ occurrance ++ } } occurrance } func main() { data := []int{12, 45, 88, 42, 0, 98, 102, 42, 77, 42, 1, 8, 7, 55, 4, 12, 87, 90, 42, 42, 11, 2, 6, 53, 90, 100, 4, 32, 8} var num int fmt.Scanln(&num) ch1 := make(chan int) ch2 := make(chan int) go count(num, data[:len(data)/2], ch1) go count(num, data[len(data)/2:], ch2) fmt.Println(<-ch1 + <-ch2) } The compiler throws these errors: line: 5:1 syntax error: syntax error: non declaration statement inside function body

8th Dec 2022, 3:08 PM
Lollo
2 Respostas
+ 3
There are a few errors in the code you provided. Here is the corrected code: The main issues with the original code were: The go keyword was used incorrectly in the definition of the count() function. The occurrence variable was misspelled as occurrance. The count() function did not use the ch channel to return the result. The main() function did not properly handle the returned values from the count() function. https://code.sololearn.com/cuaqeq8O8FS1/?ref=app
8th Dec 2022, 3:22 PM
Calviղ
Calviղ - avatar
+ 1
the link has the correct code
17th Aug 2023, 8:36 PM
Prince Otieno
Prince Otieno - avatar