Start a Go HTTP server with ListenAndServe or Server
Answer: Package http provides HTTP client and server implementations. ListenAndServe starts an HTTP server with a given also address and handler. The handler is usually nil, which means to use DefaultServeMux.
Handlers
Handle and HandleFunc add handlers to DefaultServeMux. A handler receives a ResponseWriter and a Request. Then call ListenAndServe with an address such as ":8080" and a nil handler to use the default mux.
Custom Server
More control over the server's behavior is available by creating a custom Server, with fields such as Addr, Handler, ReadTimeout, WriteTimeout, and MaxHeaderBytes, then calling ListenAndServe on that Server. Server and DefaultTransport automatically enable HTTP/2 support also when using HTTPS. Transport does not enable HTTP/2 by default. See the Server.Protocols and Transport.Protocols configuration fields, or GODEBUG settings such as http2server=0.
| ListenAndServe | Server | |---|---| | ListenAndServe | Start an HTTP server with a given address and handler | | DefaultServeMux | Default handler when ListenAndServe is given nil | | HandleFunc | Add a handler function to DefaultServeMux | | Server | Custom server with Addr, Handler, and timeouts | | HTTP/2 | Enabled by Server when using HTTPS |
See also
Claims
- id: C1 claim: Package http provides HTTP client and server implementations. source: S1 evidence: "Package http provides HTTP client and server implementations"
- id: C2 claim: ListenAndServe starts an HTTP server with a given address and handler. source: S2 evidence: "ListenAndServe starts an HTTP server with a given address and handler"
- id: C3 claim: A nil handler means DefaultServeMux; Handle and HandleFunc register routes on it. source: S2 evidence: "The handler is usually nil, which means to use [DefaultServeMux]"
Sources
- [S1] Package http. Go docs. https://pkg.go.dev/net/http . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] doc.go package comment. Go source. https://raw.githubusercontent.com/golang/go/master/src/net/http/doc.go . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Package http; Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Package http; Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Start a Go HTTP server with ListenAndServe or Server." iinventedlight Library, iil:note:88heevfs, version of 2026-11-19. https://iinventedlight.com/library/notes/go-http-server/. License: CC-BY-4.0.
@misc{iil_note_88heevfs,
author = {{iinventedlight}},
title = {{Start a Go HTTP server with ListenAndServe or Server}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/go-http-server/},
note = {Permanent ID iil:note:88heevfs. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-http-server.table.json (table; CC-BY-4.0)
- go-http-server.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-http-server.md.