Created
October 26, 2017 22:37
-
-
Save lamarmarshall/c96612688dfc72b73090b7e21207709e to your computer and use it in GitHub Desktop.
go thread select chan
This file contains hidden or 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() { | |
// 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