liveBook · Manning

Server

As explained in Learn Go with Pocket-Sized Projects

An HTTP server is software that accepts HTTP requests and returns responses over the HTTP protocol.

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

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Works through listening, handlers, routing, and response tests; leaves containerization and deployment outside scope.
Examples
Runs a minimal localhost greeting server, then develops game API endpoints and an HTML page backed by gRPC.
What it assumes
Assumes familiarity with Go modules and packages while defining server, web service, endpoint, and handler terminology.
What you can do after
Build it from scratch with Go net/http, then organize handlers and test their responses.
How it connects
Connects HTTP handlers and endpoints to routing, HTML responses, testing, and a separate gRPC service.

Also explained in

Compare how 11 books explain server →

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.