Go ships with production-ready networking in its standard library. The net package handles TCP, UDP and Unix sockets, net/http provides HTTP clients and servers (including HTTP/2 and TLS), and net/rpc, net/smtp and net/url cover related protocols. For most web services you need no framework at all.
This guide shows a TCP server, a JSON HTTP server using the method-aware router added in Go 1.22, a client with timeouts, and the settings that make these safe in production.
| Package | Purpose |
|---|---|
net | Low-level TCP, UDP, Unix sockets, DNS lookups, listeners and connections |
net/http | HTTP/1.1 and HTTP/2 clients and servers, routing, cookies, TLS |
net/url | Parse and build URLs and query strings |
net/http/httptest | Test servers and recorders for handlers |
net/netip | Compact, comparable IP address and prefix types |
crypto/tls | TLS configuration for clients and servers |
log/slog | Structured logging for request handling |
WebSockets are not in the standard library; teams typically use a maintained third-party package for them. gRPC is also a separate module.
A TCP server listens, accepts connections and handles each one in its own goroutine. Goroutines are cheap, so one per connection is the standard model.
Test it with nc localhost 9000: every line you type comes back in upper case. For UDP use net.ListenPacket("udp", ":9000") and ReadFrom/WriteTo. In real services also set deadlines with conn.SetDeadline so a stalled peer cannot hold a goroutine forever.
Since Go 1.22, http.ServeMux supports HTTP methods and path parameters in patterns, so a small API needs no router dependency.
r.PathValue("id") reads the {id} segment, and a request with the wrong method receives 405 Method Not Allowed automatically. Do not use http.ListenAndServe directly in production, because the default server has no timeouts and is open to slow-client attacks. Always build an http.Server as shown.
The package-level http.Get uses http.DefaultClient, which has no timeout. Create your own client and pass a context.
Reuse one client for the whole program so its transport can keep connections alive, and always close the response body.
A server should finish in-flight requests when it receives a termination signal:
For HTTPS call srv.ListenAndServeTLS(certFile, keyFile). Go negotiates HTTP/2 automatically on TLS connections. Set TLSConfig.MinVersion to tls.VersionTLS12 or higher.
ReadHeaderTimeout, ReadTimeout, WriteTimeout and IdleTimeout on every server.http.MaxBytesReader.http.Client with a timeout and pass context.Context everywhere.Server.Shutdown and log with log/slog.httptest.NewRecorder rather than a real port.Not for most services. Since Go 1.22 the standard ServeMux handles methods and path parameters. Frameworks add conveniences such as binding and middleware chains, but they are optional.
No. Use a maintained third-party WebSocket package. The standard library handles the HTTP upgrade request but not the framing.
http.Get safe to use?It is fine in scripts, but it uses a client without a timeout. In services, create an http.Client with a timeout.
Each connection runs in its own goroutine, and the runtime multiplexes them over OS threads using a network poller, so tens of thousands of concurrent connections are practical.
Go's net and net/http packages cover TCP, UDP, HTTP servers, clients and TLS without external dependencies. Use the Go 1.22 ServeMux patterns for routing, configure server and client timeouts, pass contexts, and shut down gracefully. Reach for third-party modules only for WebSockets, gRPC or features you genuinely need.