Last active
October 16, 2018 10:52
-
-
Save jreisinger/84ca999d6a0a2531c34b28e2a0f14141 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // There's no "channel broadcast" in Go, i.e. you can't send something to multiple goroutines using the same channel. However you can close the channel and all goroutines will be notified. | |
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| func printer(msg string, goCh chan bool) { | |
| <-goCh | |
| fmt.Printf("%s\n", msg) | |
| } | |
| func main() { | |
| goCh := make(chan bool) | |
| for i := 0; i < 10; i++ { | |
| go printer(fmt.Sprintf("printer: %d", i), goCh) | |
| } | |
| time.Sleep(time.Second * 2) | |
| close(goCh) | |
| time.Sleep(time.Second * 2) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment