liveBook · Manning

Server

As explained in Go in Practice, Second Edition

A web server is software that listens for network requests and returns responses over web protocols such as HTTP.

A Go HTTP server can accept browser requests directly and return responses without a separate web server in front of the application. The standard library's net/http package supplies the listener and request handlers. Go handles connections concurrently using goroutines, which the runtime manages across application threads. section 1.3.4 section 1.5

Serve a response on localhost

Initialize a module with `go mod init hellogo`, then save this as `main.go`: package main import ( "fmt" "net/http" ) func hello(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, my name is Inigo Montoya") } func main() { http.HandleFunc("/", hello) http.ListenAndServe("localhost:4000", nil) } Run `go run main.go` and open `http://localhost:4000/` in a browser. `HandleFunc` associates the `/` path with a handler; `ListenAndServe` starts listening. The handler receives a request and writes its response through `http.ResponseWriter`. This minimal example does not check the error returned by `ListenAndServe`. section 1.5

The listener is only a starting point. Routing matches paths to the right handlers; middleware can collect common tasks instead of duplicating them in each handler. A command-line server also needs configuration and deliberate startup and shutdown. For larger applications, plan how these building blocks meet real traffic and operational requirements before adding a framework: a framework may provide useful features, but can also add unneeded functionality, latency, or security risks. section 2.0 section 8.0

Handlers can grow beyond a greeting. They can accept form data and uploads, serve files or custom error pages, and stream incoming data. An API server can also be one component of a larger service: for example, it can accept a media upload, store the file, and queue a transcoding job. section 10.3 section 12.2

How Go in Practice, Second Edition covers it

How deep it goes
Works through a runnable listener and handler, then identifies routing, middleware, startup, shutdown, and production planning without detailing their implementations here.
Examples
Uses a localhost greeting server and a media-upload API that stores files and queues transcoding jobs.
What it assumes
Introduces module setup, imports, and handler arguments; production-oriented work builds on networking and concurrency.
What you can do after
Build it from scratch with Go's net/http, registering a path handler and starting a localhost listener.
How it connects
Connects HTTP handlers with path routing, middleware, goroutines, form processing, file serving, and microservices.

Also explained in

Compare how 11 books explain server →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.