Go Tutorial: HTTP in Go
Build web servers and make HTTP requests. Go's net/http package is all you need — no framework required.
The web is built on HTTP
Every web app, every REST API, every webhook — it all runs over HTTP. In most languages you need a framework just to get started. In Go, the standard library handles it all.
`net/http` gives you a full HTTP server and client in one package. No Express, no Django, no Spring — just Go.
You will build a server, handle routes, decode request bodies, respond with JSON, write middleware, and make HTTP requests to external APIs.
Thirteen steps.
What you'll learn in this Go http in go tutorial
This interactive Go tutorial has 9 hands-on exercises. Estimated time: 26 minutes.
- Your first library endpoint — An HTTP handler is just a function that takes a request and writes a response. Run this — it creates a handler that resp…
- Writing a search response — Add a `searchHandler` that sets `Content-Type` to `text/plain` and responds with "Searching for: [query]" where the quer…
- Status codes — book availability — Use `w.WriteHeader(http.StatusNotFound)` for a 404 when a book is not found. The starter handles ISBN "978-0141036144" —…
- Responding with JSON — book catalog — Add a second book to the catalog map (ISBN "978-0061120084"), then handle it in `bookHandler` and return JSON with `json…
- Multiple routes — library endpoints — Register multiple routes with `http.HandleFunc` or a `http.ServeMux`. Add a `/members` route that responds with "List of…
- Method checking — GET vs POST for books — REST APIs use different HTTP methods. Check `r.Method` with a switch — add `case http.MethodPost` that responds with "Ad…
- Reading a request body — check out a book — Decode a POST body with `json.NewDecoder(r.Body).Decode(&v)`. Always `defer r.Body.Close()`. Run this and change the mem…
- Middleware — logging library requests — Middleware wraps a handler to run logic before and/or after it. Add a `timingMiddleware` that prints "Response sent" aft…
- Build a mini library REST API — Build a mini REST API for a library catalog. Define a `Book` struct, create a catalog slice, write `listHandler` and `ad…
Go HTTP in Go concepts covered
- The web is built on HTTP