Implement a Go health check endpoint by creating an HTTP handler that returns a 200 status code and registering it at a specific path like /health.
Implement a health check endpoint by defining a handler that returns HTTP 200 and registering it with your HTTP server.
package main
import (
"log"
"net/http"
)
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func main() {
http.HandleFunc("/health", healthHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
A health check endpoint is a specific URL your application exposes to tell external systems if it is running correctly. Load balancers and monitoring tools hit this URL regularly; if it returns a success code, they know your app is alive and ready to handle traffic. Think of it as a digital 'thumbs up' signal that your service is operational.