To create a custom route in a Go HTTP server, we can use the http.HandleFunc()
function to register a route and its corresponding handler function.
Here's an example:
main.go333 chars18 lines
In this example, we've defined a custom route /hello
and its handler function, which simply writes Hello World!
to the response writer w
.
We then start the server on port 8080
using the http.ListenAndServe()
function.
We can test our custom route by visiting http://localhost:8080/hello
in a browser or by sending a GET request to it using a tool like curl
.
gistlibby LogSnag