Generated by GPT-5-mini| Go net/http | |
|---|---|
| Name | net/http |
| Author | |
| Released | 2009 |
| Latest | Go standard library |
| Programming language | Go |
| License | BSD-style |
| Website | Go project |
Go net/http
Go net/http is the HTTP client and server package in the Go standard library, providing primitives for building web servers, proxies, and HTTP clients. It integrates with the Go runtime and Goroutine scheduling to offer performant networking, while exposing higher-level abstractions used by projects such as Docker, Kubernetes, etcd, Prometheus, and Grafana. The package is maintained by contributors from Google and the broader open-source community, and is documented alongside other core libraries like fmt, io, and net.
net/http supplies types for HTTP requests and responses, including the HTTP request, HTTP response, and server-side primitives like Handler and ServeMux. It builds on the TCP/IP model support in net and works with TLS via integration with crypto/tls. The package facilitates common web architectures seen in deployments with Nginx, Apache HTTP Server, and cloud platforms such as Google Cloud Platform, Amazon Web Services, and Microsoft Azure. Its API design emphasizes simplicity and composability, enabling libraries like Gin (web framework), Echo (web framework), Beego, and Chi (Go). net/http’s error handling and logging patterns appear frequently in projects such as Consul and Vault.
Core types include http.Request, http.Response, http.Header, and interfaces like http.Handler and ResponseWriter. The multiplexer ServeMux maps URL patterns to Handlers; middleware chains are built by composing Handlers as in middleware used by Istio or Envoy (software). Connection management relies on the http.Transport for clients and on server-side connection state that interacts with TLS via crypto/tls. Logging and observability integrations commonly target systems like Prometheus, OpenTelemetry, and Jaeger. The package coordinates with the context package to manage cancellations, deadlines, and tracing metadata propagated from systems such as Zipkin and OpenTracing.
A typical server uses http.Server with fields for Addr, Handler, ReadTimeout, WriteTimeout, and TLSConfig; administrators tune these for deployments on Linux, FreeBSD, and Windows Server. net/http supports ListenAndServe, ListenAndServeTLS, and custom servers behind reverse proxies like HAProxy, Traefik, and Caddy. Secure deployments integrate Let's Encrypt tooling and the ACME flow handled by client libraries; enterprise setups often place net/http services behind Cloudflare or Fastly. Common configuration patterns include graceful shutdown coordinated by systemd or Kubernetes preStop hooks, health checks consumed by Prometheus, and logging structured into formats compatible with ELK Stack components like Elasticsearch, Logstash, and Kibana.
Clients create http.Client instances and configure http.Transport to control connection pooling, keep-alives, timeouts, and proxying to services such as Squid or Varnish. Transport supports HTTP/1.1 and, via TLS and ALPN, HTTP/2 negotiated with servers like Caddy and Envoy (software). Proxy support integrates with environment variables recognized by cloud-native toolchains on Heroku and Heroku Dyno environments. Advanced use includes custom RoundTripper implementations for instrumentation with OpenTelemetry or for mutual TLS connections to APIs exposed by Kubernetes API servers, GitHub API, or Stripe.
net/http leverages goroutines to serve concurrent requests; performance tuning addresses goroutine counts, memory allocation, and garbage collection parameters at the Go runtime level. High-performance services in Netflix, Uber Technologies, and Shopify-scale architectures apply connection pooling, HTTP/2 multiplexing, and load balancing with HAProxy or Envoy (software). Security practices include strict TLS configurations referencing guidance from IETF, BoringSSL, and OpenSSL policy documents, certificate management via Let's Encrypt and ACME, and authentication schemes interoperable with OAuth 2.0, OpenID Connect, and JWT (JSON Web Token). Defenses against request smuggling, CSRF, and XSS are layered with vetted middleware and platform controls used by GitLab and Bitbucket hosting solutions.
Common patterns include building REST APIs consumed by clients like curl, Postman, and Insomnia (software), serving static assets as in WordPress or Hugo-backed sites, and implementing reverse proxies and gateways for service meshes such as Istio and Linkerd. Middleware chains implement logging, metrics, authentication (OAuth 2.0, SAML), and rate limiting integrated with Redis or Memcached. Example projects demonstrating patterns include Docker, Kubernetes, Prometheus, Grafana, Etcd, Consul, and frameworks like Gin (web framework), Echo (web framework), and Chi (Go).
Category:Go (programming language) libraries