Skip to content

Instantly share code, notes, and snippets.

@shockalotti
Last active August 29, 2015 14:02
Show Gist options
  • Save shockalotti/3ef717ec0ac5b74c0ebe to your computer and use it in GitHub Desktop.
Save shockalotti/3ef717ec0ac5b74c0ebe to your computer and use it in GitHub Desktop.
Go Golang - channel select example
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