Skip to content

Instantly share code, notes, and snippets.

@kchau
Last active December 22, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kchau/6465615 to your computer and use it in GitHub Desktop.
Save kchau/6465615 to your computer and use it in GitHub Desktop.
Figuring out one channel, multiple receivers in Go...
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