Last active
August 29, 2015 14:02
-
-
Save shockalotti/3ef717ec0ac5b74c0ebe to your computer and use it in GitHub Desktop.
Go Golang - channel select example
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() { | |
c1 := make(chan string, 1) | |
c2 := make(chan string, 1) | |
go func() { | |
for { | |
c1 <- "from 1" | |
time.Sleep(time.Second *2) | |
} | |
} () | |
go func() { | |
for { | |
c2 <- "from 2" | |
time.Sleep(time.Second * 3) | |
} | |
} () | |
go func() { | |
for { | |
select { | |
case msg1 := <- c1: | |
fmt.Println("Message 1", msg1) | |
case msg2 := <- c2: | |
fmt.Println("Message 2", msg2) | |
case <- time.After(time.Second): | |
fmt.Println("timeout") | |
} | |
} | |
} () | |
var input string | |
fmt.Scanln(&input) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment