Gas stations finder in Golang
- 2 minsHi all,
I wanted to share a project I created a couple of years ago, during winter holidays, with the purpose of learning Go.
In December 2021, gas prices were more expensive than ever in Spain, and I drove a lot. That’s why I written the Gas Station Finder in Go; I wanted to find all gas stations between 2 cities and sort them by price. This way, I could fill the tank at a cheaper gas station without having to deviate from the route.
Let me break down how it all works.
The project relies on the OpenRouteService API to find the route between two coordinates. It also uses its reverse geocoding service to convert city names into coordinates. This is done to expose an API that given 2 city names, it returns both the route and all near gas stations with its prices.
The REST API is built with the Gin web framework. I liked it as it was straightforward and simple to use.
The app is split into different components: one for figuring out where you are, one for planning routes, another for getting directions, one for getting gas station prices, and another to retrieve gas stations nearby.
In order to improve performance, I did two things:
- Use a KDTree to store Gas stations. For a given route, it called the KDTree to find nearby gas stations really fast (O(log n)).
- Keep all prices in memory and refresh them in the background every 4 hours. Since the prices don´t change that often, caching them was a good idea to improve performance, because retrieving the prices from the Spanish goverment website takes quite a bit.
As an improvement, instead of having all the gas stations prices in memory, we can use an embedded database like GrausDB. It can also be used to store routes between cities instead of calling OpenRouteService to calculate them each time. These routes are not going to change often, therefore we can keep refresh them every 2 weeks, for example.
I enjoyed coding it since it only took a few hours, and it proved to be a useful app for myself. Also, I learned a little bit of Go!
You can find the code on GitHub: GasPrices.
Thanks for reading!
Ricardo.