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 12 hands-on exercises. Estimated time: 26 minutes.
- Your first HTTP server — Three lines of Go start a web server:
- Writing a response — `http.ResponseWriter` is an `io.Writer` — you can use `fmt.Fprintln`, `fmt.Fprintf`, or `w.Write()` to send a response.
- Status codes — HTTP status codes tell the client what happened:
- Responding with JSON — Most APIs respond with JSON. The pattern:
- Reading a request body — For POST and PUT requests, the client sends data in the request body.
- Multiple routes — Register multiple routes with `http.HandleFunc`:
- Method checking — GET vs POST — REST APIs use different HTTP methods for different operations:
- Middleware — Middleware is a function that wraps a handler — it runs before and/or after the handler.
- Making an HTTP GET request — Go is not just a server — it is also a great HTTP client.
- Making an HTTP POST request — `http.Post` sends a POST with a body:
- Setting request headers — Many APIs require headers — authentication tokens, content types, API keys.
- Build a mini REST API — No starter code.
Go HTTP in Go concepts covered
- The web is built on HTTP