How to Implement Health Check Endpoints in Go

Web
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))
}