You did not close ch2 in the first goroutine. Therefore, the second 
goroutine never reaches line "ch <- struct{}{}"

Aleksei Perevozov
воскресенье, 5 июня 2022 г. в 16:06:53 UTC+2, manjee...@gmail.com: 

> I just started to learn Go Language, and I'm enjoying writing code in Go.
> So today, I'm trying to understand how the unbuffered channel works
>
> Can someone help me to understand why this code is not working. I'm 
> expecting that <-ch wait until we receive from channel ch
>
>
> func main() {
>     ch := make(chan struct{})
>     ch2 := make(chan int)
>     
>     go func() {
>         arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>         for _, v := range arr {
>             ch2 <- v
>         }
>     }()
>
>     go func() {
>         for t := range ch2 {
>             fmt.Println(t)
>         }
>         ch <- struct{}{}
>     }()
>
>     <-ch
> }
>
> Thanks & Regards  
>
> Manjeet Thakur
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/7b6b468b-15e4-4ece-8185-72f586e6ae0an%40googlegroups.com.

Reply via email to