Go’s net/http package provides HTTP client and server implementations. A currency converter can use its client utilities to fetch exchange-rate data; a web service can use its server utilities to receive requests. section 6.5.3 section 8.1.2
Choose the package API that fits the job:
- For a quick outbound request, use http.Get. It uses the shared DefaultClient; check the returned error and close a successful response body. Treat data from an external service as untrusted. section 6.5.3
- For a call that must not wait indefinitely, create an http.Client with a Timeout and call its Get method. The default timeout is zero, meaning no timeout. A timed-out call can be identified through a *url.Error and its Timeout method. section 6.6.2
- For inbound requests, start with http.ListenAndServe(address, handler). A ServeMux can register handlers by URL and serve as that handler when the service needs multiple endpoints. section 8.1.2 section 8.2.1
Start a small service
Run `go mod init example/service`, save this as main.go, then run `go run .`: ```go package main import "net/http" func main() { mux := http.NewServeMux() mux.HandleFunc("/games", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { w.WriteHeader(http.StatusMethodNotAllowed) return } w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte("Creating a new game")) }) if err := http.ListenAndServe(":8080", mux); err != nil { panic(err) } } ``` Try `curl -v localhost:8080/games` for a 405 response, then `curl -v -X POST localhost:8080/games` for a 201 response. Set the status before writing the body: writing first commits the default success status. section 8.1.2 section 8.2.1
Keep client configuration local to the component making the call rather than changing the global DefaultClient. For tests, httptest.NewServer can supply a controlled response—or delay one—so success and timeout behavior can be checked without relying on the external endpoint. section 6.6.2


