gobuch/best-practices/context-http-server/main.go

25 lines
413 B
Go
Raw Permalink Normal View History

2020-08-21 04:26:40 +00:00
package main
import (
"fmt"
"net/http"
"time"
)
func handleCall(res http.ResponseWriter, req *http.Request) {
ctx := req.Context()
select {
case <-ctx.Done():
res.Write([]byte("end"))
fmt.Println("clsoing connection to client")
return
case <-time.After(20 * time.Second):
fmt.Fprintln(res, "Hello World")
}
}
func main() {
http.HandleFunc("/", handleCall)
http.ListenAndServe(":8080", nil)
}