|
Trong ví dụ trước chúng ta đã xem qua cách thiết lập một
HTTP server. HTTP servers rất hữu ích để
minh họa việc sử dụng |
![]()
package main |
import ( "fmt" "net/http" "time" ) |
|
func hello(w http.ResponseWriter, req *http.Request) { |
|
|
Một |
ctx := req.Context()
fmt.Println("server: hello handler started")
defer fmt.Println("server: hello handler ended")
|
|
Chờ một vài giây trước khi gửi một câu trả lời
cho client. Điều này có thể giả lập một số công việc
mà server đang làm. Trong khi đó, hãy quan sát
channel |
select {
case <-time.After(10 * time.Second):
fmt.Fprintf(w, "hello\n")
case <-ctx.Done():
|
|
Hàm |
err := ctx.Err()
fmt.Println("server:", err)
internalError := http.StatusInternalServerError
http.Error(w, err.Error(), internalError)
}
}
|
func main() { |
|
|
Giờ chúng ta chỉ định hàm xử lý cho đường dẫn “/hello” và bắt đầu lắng nghe các HTTP request. |
http.HandleFunc("/hello", hello)
http.ListenAndServe(":8090", nil)
}
|
|
Run the server in the background. |
$ go run context-in-http-servers.go &
|
|
Simulate a client request to |
$ curl localhost:8090/hello server: hello handler started ^C server: context canceled server: hello handler ended |
Ví dụ tiếp theo: Spawning Processes.