Last active
December 22, 2015 11:28
-
-
Save kchau/6465615 to your computer and use it in GitHub Desktop.
Figuring out one channel, multiple receivers in Go...
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
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
) | |
func main() { | |
commands := make(chan string) | |
for i := 0; i < 4; i++ { | |
go testChan(commands, i) | |
} | |
for i := 1; i < 10; i++ { | |
commands <- fmt.Sprintf("Who's doing what... %d", i) | |
} | |
time.Sleep(5) | |
} | |
func testChan(myChan chan string, id int) { | |
for msg := range myChan { | |
log.Printf("%d %s", id, msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment