Here's an example code for creating an HTTP server with a HEAD route in Go:
main.go845 chars40 lines
In this example, we define four different routes: /hello
, /health
, /hello/
(with a trailing slash), and the default route for anything else. The helloHandler
and healthCheckHandler
functions handle GET and HEAD requests, and the defaultHandler
function returns a 404 Not Found HTTP status code. The fmt.Fprint
function writes a response body to the client, and w.WriteHeader
sets the HTTP status code for the response. Finally, we use http.ListenAndServe
to start the server listening on port 8080, and pass nil
as the handler, which means we're using the default ServeMux.
gistlibby LogSnag