Skip to content

Instantly share code, notes, and snippets.

@lamarmarshall
Created October 26, 2017 22:37
Show Gist options
  • Save lamarmarshall/c96612688dfc72b73090b7e21207709e to your computer and use it in GitHub Desktop.
Save lamarmarshall/c96612688dfc72b73090b7e21207709e to your computer and use it in GitHub Desktop.
go thread select chan
package main
import (
"fmt"
"time"
)
func main() {
// first chan to return executes
select {
case v := <-streamer(3):
fmt.Println(v, "chan 1")
case v1 := <-streamer(5):
fmt.Println(v1, "chan 2")
}
}
func streamer(sec int) chan int {
ret := make(chan int)
go func() {
time.Sleep(time.Duration(sec) * time.Second)
ret <- 3
}()
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment