A perfect example of how lovely Go can be:
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
var handler http.Handler | |
select { | |
case <-(func() <-chan struct{} { | |
ch := make(chan struct{}) | |
go func() { | |
time.Sleep(6 * time.Second) | |
close(ch) | |
}() | |
return ch | |
})(): | |
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte("hello world")) | |
}) | |
case <-time.After(5 * time.Second): | |
handler = UserTemplateResponse(http.StatusGatewayTimeout, ci) | |
} | |
handler.ServeHTTP(w, r) | |
}) |
This code is from Gondor's router during spin (being ported from original Python so not complete yet.)