Created
January 20, 2015 13:34
-
-
Save tbillington/4763eff73831628f7a47 to your computer and use it in GitHub Desktop.
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
var chans = make(map[string]*chan string) | |
func Server() { | |
println("started server") | |
for { | |
var timeout = make(chan string, 1) | |
go func() { | |
time.Sleep(1 * time.Second) | |
timeout <- "timeout!" | |
}() | |
chans["timeout"] = &timeout | |
cases := make([]reflect.SelectCase, len(chans)) | |
// Make array from the map values | |
var values = make([]*chan string, 0) | |
for _, value := range chans { | |
values = append(values, value) | |
} | |
for i, ch := range values { | |
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)} | |
} | |
_, value, ok := reflect.Select(cases) | |
if ok { | |
println(value.String()) | |
} else { | |
println("something closed?") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment