Skip to content

Instantly share code, notes, and snippets.

@tbillington
Created January 20, 2015 13:34
Show Gist options
  • Save tbillington/4763eff73831628f7a47 to your computer and use it in GitHub Desktop.
Save tbillington/4763eff73831628f7a47 to your computer and use it in GitHub Desktop.
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