Go handles networking with the standard library packages net for TCP, UDP and Unix sockets, net/http for HTTP clients and servers, and crypto/tls for encryption. Each accepted connection is typically served by its own goroutine, so you write simple blocking code and the runtime multiplexes it onto an efficient network poller. For WebSocket and gRPC you add well-known third-party modules.
This guide covers TCP, UDP, HTTP servers and clients, and the practices that make network code reliable: timeouts, contexts and graceful shutdown.
net.Listen creates a listener, Accept returns a net.Conn, and you read and write on the connection like a file. Blocking calls are cheap because goroutines are cheap and the runtime uses epoll, kqueue or IOCP internally.
Test it with nc localhost 9000. Deadlines on the connection stop idle or stuck clients from holding goroutines forever. For production, track active connections with a sync.WaitGroup so shutdown can wait for them.
A TCP client uses net.Dialer, which supports timeouts and contexts:
UDP is connectionless, so you read datagrams with ReadFrom and reply with WriteTo.
UDP gives no delivery or ordering guarantee, so use it for DNS-like lookups, metrics, discovery or real-time data where loss is acceptable.
Since Go 1.22 the standard http.ServeMux supports methods and path parameters in patterns.
Never use the zero-value http.Server or bare http.ListenAndServe in production; without timeouts, slow clients can exhaust your resources.
The default client has no timeout. Create one, and reuse it, because it pools connections:
The snippet needs the fmt and io imports. Always close the body, check the status code and limit how much you read.
http.ListenAndServeTLS or tls.Config on a listener. Keep MinVersion at tls.VersionTLS12 or higher, and use golang.org/x/crypto/acme/autocert for automatic certificates. HTTP/2 is enabled automatically over TLS, and Go 1.24 and later also support configuring protocols with http.Server.Protocols.github.com/coder/websocket or github.com/gorilla/websocket.google.golang.org/grpc with Protocol Buffers for typed, streaming service-to-service calls.net.Resolver and net.Dialer give control over DNS and timeouts.context.Context to dial and request calls so work can be cancelled.Server.Shutdown, and wait for in-flight work.http.Client and its transport; tune Transport.MaxIdleConnsPerHost for busy clients.http.MaxBytesReader to defend against abuse.errors.Is and errors.As, for example net.ErrClosed and os.ErrDeadlineExceeded, and use net.Error to detect timeouts.net/http/httptest and observe with metrics, structured logs and net/http/pprof on an internal port.Usually yes. It is simple and scales to tens of thousands of connections because goroutines are lightweight and the runtime uses non-blocking I/O underneath.
Not required. The standard net/http with the Go 1.22 router handles many services. Frameworks add conveniences such as binding and middleware chains.
Set http.Client.Timeout for the whole request, or use http.NewRequestWithContext with a context that has a deadline for per-call control.
No. The golang.org/x/net/websocket package exists but is not recommended; use coder/websocket or gorilla/websocket.
Go's net and net/http packages give you TCP, UDP and HTTP with simple blocking code. Set timeouts, use contexts, shut down gracefully, reuse clients and secure connections with TLS. Add third-party modules for WebSocket and gRPC when you need them.