Go's standard library is well suited to cloud and serverless workloads because it ships a production-grade HTTP server and client, structured logging, context for deadlines and cancellation, and TLS, all inside a single static binary. You add cloud provider SDKs for storage, queues and databases, and you package the binary for a container platform or a function runtime.
The main techniques are: configure from environment variables, set timeouts on everything, shut down gracefully, expose health checks, and choose the deployment target that matches your traffic pattern.
A good cloud service has timeouts, a health endpoint, structured logs and graceful shutdown. Go 1.22 added method and path patterns to http.ServeMux, so no router package is required.
The older pattern http.ListenAndServe(":8080", nil) has no timeouts, so slow clients can hold connections open indefinitely. Cloud platforms send SIGTERM before stopping a container, and Shutdown lets in-flight requests finish. JSON logs on stdout are what most platforms collect.
Read configuration from environment variables, fail fast when required values are missing, and never log secrets.
The original example printed the full database URL, which usually contains a password. Keep secrets in a secrets manager and inject them at runtime.
Calls to other cloud services fail and stall. Give each outgoing request a deadline, and run independent calls in parallel with golang.org/x/sync/errgroup.
Each goroutine writes only to its own slice index, so no lock is needed. The shared context cancels the remaining calls after the first error or after three seconds.
A multi-stage build produces a small image with a static binary:
The same image runs on Kubernetes, Amazon ECS, Google Cloud Run and Azure Container Apps. Pin the Go image version to your go.mod requirement.
| Platform | How Go runs |
|---|---|
| AWS Lambda | Use the provided.al2023 custom runtime with a binary named bootstrap, built with the aws-lambda-go library. The older go1.x managed runtime is deprecated. |
| Google Cloud Functions and Cloud Run | Go is supported natively. Cloud Run runs any container that listens on PORT. |
| Azure Functions | Run Go as a custom handler: an HTTP server that the Functions host forwards requests to. |
Serverless suits spiky or low traffic. Go's fast cold starts and small binaries help, but keep initialisation work, such as creating SDK clients, outside the request handler so warm invocations reuse it.
Use the official SDK for each provider rather than hand-written REST calls: the AWS SDK for Go v2, the Google Cloud client libraries and the Azure SDK for Go. They handle authentication, retries and pagination. Pass a context to every call, and prefer short-lived credentials from the platform's identity system over static keys.
Yes. Go starts quickly and uses little memory, which keeps cold starts and cost low on platforms such as AWS Lambda and Cloud Run.
Not necessarily. net/http with the Go 1.22 ServeMux covers routing by method and path, and you can add packages only where needed.
Build a Linux binary named bootstrap, use the provided.al2023 runtime, and write the handler with the aws-lambda-go library.
Read them from environment variables injected by a secrets manager, never commit them, and avoid logging them.
For cloud and serverless computing, Go's standard library gives you HTTP, TLS, JSON, logging and context. Set timeouts, shut down gracefully, configure from the environment, and use provider SDKs for managed services. Package the static binary as a container or a function, depending on your traffic.