Deadlock error issue

https://play.golang.org/p/9uWepL4nu2n
Im putting items into my channel, and pulling from diff goroutines.
After i pull everything needed from the channel, i get a deadlock error.
using a normal for loop like

for i := 0; i < *n; i++ {
  	res := <-c
  	fmt.Printf("[%s  %v]\n", res.label, res.time.Format("15: 04: 05.0000"))
  }

works, but i need to work with it using range

any idea whats wrong with the code?
i think the issue is finding where i can close my channel
https://play.golang.org/p/9uWepL4nu2n

Yep it’s common. You can waiting and close in separate goroutine:

	go func() {
		wg.Wait()
		close(c)
	}()
	consumer(c)
1 Like

Thanks a lot. Couldnt have figured it out myself

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.