Last active
November 27, 2023 01:16
-
-
Save kkamara/b773b33601503674f7feb7aa6c6c338f to your computer and use it in GitHub Desktop.
closing channels go routines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func producer(chnl chan int) { | |
for i := 0; i < 10; i++ { | |
chnl <- i | |
} | |
close(chnl) | |
} | |
func main() { | |
ch := make(chan int) | |
go producer(ch) | |
for { | |
v, ok := <-ch | |
if ok == false { | |
break | |
} | |
fmt.Println("Received ", v, ok) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment