mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-23 01:47:08 +08:00
33 lines
586 B
Go
33 lines
586 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type key string
|
|
|
|
const isValidHostKey key = "isValidHost"
|
|
|
|
func checkValid(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
validHost := r.Host == "acme"
|
|
ctx := context.WithValue(r.Context(), isValidHostKey, validHost)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func handler(ctx context.Context, ch chan Message) error {
|
|
for {
|
|
select {
|
|
case msg := <-ch:
|
|
// Do something with msg
|
|
_ = msg
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
}
|
|
|
|
type Message struct{}
|