An example of a simple REST endpoint in go is:
Choosing
gorilla/mux
yields a cleaner, simpler interface that works well with the standard library. From main()
instantiate the router, for handling the HTTP routes, initialize it, and finally listen and serve.
The router instantiated is the mux.Router
that we will use to register routes to be served. Since this is just an example nothing more sophisticated than serving a GET
endpoint at /info
is happening. From there initialize the router by registering the routes and fire up the HTTP server using the standard library’s http.Server()
.
Run the endpoint with go run
from the application directory and fire off a curl request or open a browser to see the returned JSON.