Go's `net/http`: A Production-Ready Web Server
Go's net/http package provides a powerful, production-ready web server without external frameworks. You build services by creating handlers—functions that process a request and write a response. It's ideal for APIs and microservices.
Why it exists
Go was designed for building networked services. The net/http package provides a robust, high-performance HTTP server directly in the standard library, enabling developers to create web applications and APIs without relying on third-party frameworks. This aligns with Go's philosophy of providing simple, powerful primitives.
The mental model
Think of net/http not as a rigid framework, but as a toolkit of building blocks. The central concept is the http.Handler interface. A handler is any object that has a ServeHTTP(w http.ResponseWriter, r *http.Request) method. Your entire web service is just a composition of these handlers, each responsible for a piece of logic.
How it works
You start a server with http.ListenAndServe(":8080", myMux). This function listens for TCP connections on a given address and port. For each incoming request, it calls the ServeHTTP method of the handler you provided (myMux). The handler is usually a router, called a ServeMux in Go. You register URL patterns and their corresponding handlers on this mux. For example, myMux.HandleFunc("/users", handleGetUser) tells the mux to execute the handleGetUser function for requests to the /users path.
When to use it
Use net/http for building APIs, microservices, and simple web applications where you want full control and minimal dependencies. It's perfect for performance-critical services because there's very little overhead between the network connection and your code. It is the idiomatic starting point for most Go web services.
When not to use it
For large applications, a full-featured framework might be more productive. The standard library's ServeMux is limited; it doesn't support path parameters (like /users/:id) or HTTP method-based routing out of the box. While you can build this yourself, using a third-party router is common practice.
One canonical example
The simplest server uses http.HandleFunc to register a handler on the default ServeMux. Calling http.ListenAndServe(":8080", nil) starts the server, with nil telling it to use this default mux. The biggest footgun is this global ServeMux; any imported package can register a route on it, potentially causing conflicts or security issues. Best practice is to create your own mux with http.NewServeMux() for explicit control.
Interview question
When is Go's `net/http` package generally the most suitable choice?
- a.For building large, complex web applications requiring extensive built-in features.
- b.For applications where a global routing configuration is preferred for ease of setup.
- c.When needing advanced routing capabilities like path parameters and method-based routing out-of-the-box.
- d.When developing performance-critical APIs and microservices with minimal dependencies.Correct
Why? this is the answer
The card explicitly states `net/http` is "perfect for performance-critical services" and "ideal for APIs and microservices" with "minimal dependencies". Option C is incorrect because the standard library's ServeMux lacks advanced routing features like path parameters out of the box.
Just read this? Test yourself on what you have been reading.
Read the original → pkg.go.dev
- #go
- #backend
- #networking
- #web-server
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.
See open roles