Go HTTP Server
Minimal HTTP server with routing and JSON response in Go.
package main
import (
"encoding/json"
"log"
"net/http"
)
type Response struct {
Message string `json:"message"`
Status int `json:"status"`
}
func jsonResponse(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
jsonResponse(w, http.StatusOK, Response{Message: "ok", Status: 200})
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/health", healthHandler)
log.Println("Server running on :8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}