Go's net package provides TCP, UDP and Unix socket networking, and net/http builds HTTP clients and servers on top of it. You open a connection with net.Dial, accept connections with net.Listen, and handle each client in its own goroutine. Always set deadlines and close connections.
This guide covers a TCP server and client, UDP, timeouts, TLS, HTTP and graceful shutdown.
| Package | Purpose |
|---|---|
net | TCP, UDP, Unix sockets, DNS lookups, IP and address types |
net/http | HTTP/1.1 and HTTP/2 clients and servers |
crypto/tls | TLS for any net.Conn |
net/netip | Compact, comparable IP address types |
net/rpc, net/smtp | Older helpers, mostly frozen. Prefer gRPC or a maintained mail library |
golang.org/x/net | Extra protocols such as WebSocket-related and HTTP/2 helpers |
net.Listen returns a net.Listener. The server loops on Accept and handles each connection in a goroutine. Read with a bufio.Scanner or bufio.Reader, because a single Read call does not guarantee a whole message.
TCP is a byte stream, not a message stream. Frame your messages with newlines, a length prefix or a protocol such as HTTP.
net.DialTimeout stops a client waiting forever on an unreachable host.
For cancellation tied to a context, use (&net.Dialer{}).DialContext(ctx, "tcp", addr).
UDP is connectionless and unreliable, so datagrams may be lost, reordered or duplicated. Use net.ListenPacket for a server and net.Dial("udp", ...) for a client.
Use UDP for DNS-style queries, telemetry or real-time media, and add your own acknowledgements if you need reliability.
Wrap a listener with tls.NewListener or use tls.Listen. Set a minimum version, and never disable certificate verification in production.
This needs the crypto/tls and log imports. On the client side, tls.Dial verifies the server certificate by default. Do not set InsecureSkipVerify outside tests.
For web services, use net/http rather than raw sockets. Configure timeouts to protect against slow clients, and shut down cleanly.
Method and wildcard patterns such as GET /hello/{name} are supported in http.ServeMux since Go 1.22.
http.Client. The default client has no timeout.errors.Is and net.Error, then retry with backoff.io.LimitReader or http.MaxBytesReader.defer conn.Close() and release resources in every error path.log/slog for structured connection logs, and metrics for connection counts and latency.net.Pipe for in-memory connections and httptest for HTTP.Use bufio.Reader or io.ReadFull with a framing rule such as a delimiter or a length prefix. A single Read can return partial data.
Not in the standard library. Use a maintained package such as github.com/coder/websocket or github.com/gorilla/websocket.
Dial opens an outgoing connection as a client, while Listen waits for incoming connections as a server.
Call conn.RemoteAddr() on a connection, or parse r.RemoteAddr in an HTTP handler. Behind a proxy, read forwarding headers only from trusted proxies.
Go's net package gives you TCP, UDP and Unix sockets, with net/http and crypto/tls layered on top. Handle each connection in a goroutine, frame your messages, set deadlines, secure traffic with TLS and shut servers down gracefully.