How to Add Health Check Endpoints in Go

Web
Add a /health handler to your Go HTTP server that returns a 200 OK status to confirm the application is running.

Add a handler to your http.Server that returns a 200 status code to indicate the application is healthy.

package main

import (
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("OK"))
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Start your server and verify the endpoint by visiting http://localhost:8080/health.