liveBook · Manning

Net/http package

As explained in Learn Go with Pocket-Sized Projects

net/http is Go's standard-library package providing HTTP client and server implementations.

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

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Works through client timeouts, server startup, routing, handlers, status codes, and tests without detailing HTTP transport internals.
Examples
Uses an exchange-rate endpoint, a delayed test server, and a game-creation endpoint exercised with curl.
What it assumes
Assumes basic Go program and module use while introducing the client and server APIs through examples.
What you can do after
Build it from scratch: make a time-bounded HTTP call or serve and test a routed endpoint with net/http.
How it connects
Connects HTTP clients to timeouts and httptest, and HTTP servers to handlers, ServeMux, request methods, and status codes.

Also explained in

Compare how 3 books explain net/http package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.