Go's standard library covers most network programming without third-party packages. Use net for TCP, UDP, Unix sockets and DNS, net/http for HTTP servers and clients, net/url for URL parsing, net/netip for IP addresses, crypto/tls for encryption and net/rpc or net/smtp for older protocols. Every blocking call can be bounded with deadlines or a context.Context.
This guide shows the production-ready way to use each package: timeouts everywhere, graceful shutdown, structured logging with log/slog, and the patterns that replace older examples that leak goroutines or hang forever.
| Task | Package | Notes |
|---|---|---|
| TCP, UDP, Unix sockets | net | net.Listen, net.Dialer, net.ListenPacket |
| HTTP server and client | net/http | HTTP/1.1 and HTTP/2 built in |
| URL and query handling | net/url | url.Parse, url.Values |
| IP addresses and prefixes | net/netip | Comparable, allocation-free value types |
| TLS | crypto/tls | Used by net/http and usable on raw sockets |
| DNS lookups | net | net.Resolver with context support |
| Mail sending | net/smtp | Frozen; prefer a maintained module for new projects |
| RPC | net/rpc | Frozen; prefer gRPC or JSON over HTTP for new systems |
A raw TCP server needs three things the short examples skip: a read deadline so idle clients cannot hold a goroutine forever, a way to stop accepting connections, and logging that records errors rather than printing them.
The listener is closed when the process receives Ctrl+C, Accept returns net.ErrClosed, and the loop waits for in-flight sessions. Each connection runs in its own goroutine, which is cheap in Go. Deadlines are absolute times, so they are renewed before every read and write.
net.Dial has no timeout by default, so always use a net.Dialer with DialContext or set Dialer.Timeout.
UDP has no connections, so you read datagrams and reply to the sender address.
DNS lookups go through net.DefaultResolver or a custom net.Resolver, both of which accept a context:
For IP math, use net/netip. netip.Addr and netip.Prefix are comparable value types that work as map keys, unlike net.IP.
Since Go 1.22, http.ServeMux supports methods and path wildcards, so many services no longer need a router package.
Calling http.ListenAndServe directly uses a server with no timeouts, which leaves it open to slow-client attacks. Always build an http.Server and set at least ReadHeaderTimeout.
http.DefaultClient has no timeout. Create one client, reuse it, and pass a context per request.
Always close the body, check the status code (a 404 is not an error to client.Do), and cap how much you read. Reusing the client lets the underlying http.Transport pool connections.
Serve HTTPS with srv.ListenAndServeTLS(certFile, keyFile). For raw sockets use tls.Listen and tls.Dial. Set a minimum version explicitly when you configure TLS yourself:
Never set InsecureSkipVerify: true outside local tests, because it disables certificate validation.
Not for most services. Since Go 1.22, http.ServeMux handles methods and {id} wildcards, and encoding/json, log/slog and context cover the rest.
http.DefaultClient and net.Dial have no timeout. Set Client.Timeout, use http.NewRequestWithContext, or dial with a net.Dialer and a context.
Both packages are frozen and receive no new features. Prefer gRPC or plain JSON over HTTP for RPC, and a maintained mail-sending library or a provider API for email.
Close the listener to make Accept return net.ErrClosed, then wait for active connections with a sync.WaitGroup. For HTTP use Server.Shutdown.
Use net for TCP, UDP and DNS, net/http for HTTP, and crypto/tls for encryption. Set deadlines or contexts on every call, configure http.Server timeouts, reuse a single http.Client, close response bodies, and shut servers down gracefully on a signal.