Hello .. thanks for the video. But actually you didn't need waitgroups here. as long as you are iterating through the ch values, you could simply use an integer counter, which increments each time you read, then a simple if condition to check if the read becomes = to the go-routines you've spawned then you can close the channel. counter := 0 for msg := range ch { log.Println(msg) counter++ if counter == 2 { close(ch) } }
Nice video🙏🏽. Also could you explain what you meant over here? I couldn’t really understand you. You said when we use channels in this manner, what? 15:08 Thanks in advance for your explanation.
@TiagoTaquelim How are we able to range over a channel after closing it? Would it add any benefit if we executed wg.Wait() and close(ch) in a separate goroutine?
Hello .. thanks for the video. But actually you didn't need waitgroups here.
as long as you are iterating through the ch values, you could simply use an integer counter, which increments each time you read, then a simple if condition to check if the read becomes = to the go-routines you've spawned then you can close the channel.
counter := 0
for msg := range ch {
log.Println(msg)
counter++
if counter == 2 {
close(ch)
}
}
Thank you so much for showing the errors and ways things can go wrong! Very insightful! :]
Nice work! 👍 thanks for the video!
Thanks for sharing Bud.
Thanks!
Nice video🙏🏽. Also could you explain what you meant over here? I couldn’t really understand you.
You said when we use channels in this manner, what?
15:08
Thanks in advance for your explanation.
Great video! Thanks
Hey Tiago, great video man. Could you please share your theme? Or is this a custom highlighting?
Thanks! It's called Gruvebox.
@TiagoTaquelim How are we able to range over a channel after closing it? Would it add any benefit if we executed wg.Wait() and close(ch) in a separate goroutine?
Hey! wg.Wait is syncrhonous (it blocks the flow of the program) it cannot be in a goroutine.
We cannot range a closed channel, it will panic.