Skip to content

Instantly share code, notes, and snippets.

@mannion007
Created February 29, 2020 17:16
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 mannion007/8c4230e2310b951b014760851ea49c8b to your computer and use it in GitHub Desktop.
Save mannion007/8c4230e2310b951b014760851ea49c8b to your computer and use it in GitHub Desktop.
Channel select
package main
import (
"fmt"
"time"
)
func main() {
slowChan := make(chan string)
fastChan := make(chan string)
go func() {
for {
slowChan <- "message from slow worker"
time.Sleep(time.Duration(2) * time.Second)
}
close(slowChan)
}()
go func() {
for {
fastChan <- "message from fast worker"
time.Sleep(time.Duration(500) * time.Millisecond)
}
close(fastChan)
}()
for {
select {
case out := <-slowChan:
fmt.Println(out)
case out := <-fastChan:
fmt.Println(out)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment