PaperCall is shutting down on August 31, 2026. Existing events and submissions will remain available until then. New events can no longer be created.

HTTP Server on random available port in Go

By Kazuki Higashiguchi

Elevator Pitch

Imagine you want to start an HTTP server without specifying the port. For example, a temporary server for testing. How do you do that? I’ll give you a way to implement this design and a clear understanding of what Go does inside the net package.

Description

Imagine you want to start an HTTP server without specifying the port. For example, a temporary server for testing. How do you do that?

The answer is straightforward. Specify the port number to 0 like:

l, err := net.Listen("tcp", ":0")

It’s an easy rule provided by the net package, but do you understand how Go binds a random port allocation. I’ll give you a clear understanding of what Go does inside the net package.

The presentation contains the following topics.

  • net, net/http packages
  • File descriptor
  • TCP, UDP
  • System calls

I think this presentation will give audiences not only a knowledge of Go, but also one of system calls.

Notes

The content of this proposal was already published on dev.to.

https://dev.to/hgsgtk/how-go-handles-network-and-system-calls-when-tcp-server-1nbd