An HTTP server listens on a port, receives requests, and sends responses. A web service keeps that server running so clients can reach its API. Endpoints provide access through URLs; HTTP handlers process the requests and produce the responses. section 8.1.1
To get a small Go server running:
- Choose a free development port, such as 8080, and start listening with net/http.ListenAndServe. A running service normally keeps this call from returning. section 8.1.2
- Register a handler for a path. Its http.ResponseWriter sends the response; its *http.Request supplies the request. section 11.1
- Run the program and visit its local URL in a browser, or send a request with curl. section 11.1 section 8.1.2
Listing 11.1 Minimal HTTP server
func main() {
http.HandleFunc("/",
func(w http.ResponseWriter, r *http.Request) { #1
io.WriteString(w, "ሰላም፣ሰዎች።") #2
})
err := http.ListenAndServe(":8083", nil) #3
if err != nil {
fmt.Fprintln(os.Stderr, "Error while serving: " + err.Error())
}
}
This minimal server connects a root-path handler to a response and starts listening on port 8083. Running it and opening localhost:8083 verifies the request–response path. section 11.1
As routes grow, keep main focused on startup. A Server struct can hold dependencies, while a router maps paths to handler methods. This separates endpoint behavior for testing: an httptest recorder can check the returned status code and body. section 11.1.1 section 10.2.3
Two jobs for an HTTP server
A game service can expose endpoints for creating a session and checking game status. An HTML server can instead return a page to a browser and act as a bridge to a separate gRPC service. The gRPC server listens and serves too, but registers an implementation of its generated service API rather than HTTP route handlers. section 8.0 section 11.1 section 10.2.2
Treat startup errors explicitly, and consider failure paths before deployment: the number of users and requests may be unknown. A server that works locally is a starting point, not a deployment plan. section 8.1.2 section 8.6 section 8.0










