I have been working on creating a web application with Go. I am currently making rest calls to multiple APIs that retrieve information. Once I get the data I extract, transform and load the data into a repository. I ran into a problem when I needed to pull out the path from the URL and decided to try gorilla mux. In this guide I will explain:
- Gorilla Mux
- URL params
- Passing data to template
What is Gorilla Mux?
Gorilla is a tool-kit that has packages to help with routing, session, websockets and much more. In this guide I will be talking about Gorilla Mux, which is a powerful web router and dispatcher.
Gorilla Mux Documentation — http://www.gorillatoolkit.org/pkg/mux
How do you register routes?
We need something to register the routes to, I think the mux router would be a great tool to use, so lets create a NewRouter.
Now that we have a router lets register some routes
This should look very similar to net/http, if you want to learn more about net/http check out the below link:
net/http — https://medium.com/@tommarler/go-rendering-html-templates-fff8457780db
What does the HandleFunc do?
It registers the URL path and handler, if a incoming URL request matches one of the paths it will call the handler. For I go to /home the HomePage handler will…