Created
February 29, 2020 17:16
-
-
Save mannion007/8c4230e2310b951b014760851ea49c8b to your computer and use it in GitHub Desktop.
Channel select
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" | |
"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