Build a serverless Go application by writing an HTTP handler, initializing a module, and compiling a static binary for cloud deployment.
- Create a new module and initialize your project with
go mod init serverless-app. - Write an HTTP handler in
main.gothat returns a response usinghttp.HandleFuncandhttp.ListenAndServe. - Compile the application into a static binary using
go build -o main. - Deploy the
mainbinary to a serverless platform like Google Cloud Functions or AWS Lambda.
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, Serverless Go!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}