← All notes

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

Sources

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

Permanent ID: iil:note:88heevfs

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.},
}

Markdown

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.

All artifact files (JSON index)

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.