At my day-to-day, I work mainly with Node.js. Installing npm
packages, using webpack to bundle stuff and importing named exports in a lot of .ts
files. NoSQL databases are not too far away from me too.
But in my free time I like to explore new technologies. The Go realm is one of them. When doing this, I feel like I am in a new town: getting to know new places and meeting new people.
After years working with MongoDB and having few experiences when reading and learning about Go, I thought it was time to create something to the Go community. So here it is: a minimal Go boilerplate that is equipped with Gin + MongoDB and testable with Testcontainers.
Why another-boilerplate-in-an-open-source-repository?
Starting a project from scratch in Go can be a bit painful, especially when you came from other programming languages like TypeScript.
Considering this and how I read "learn in public" sentence as a moral rule, I have decided to create a Go repository for me which contains Gin + MongoDB to accelerate the creation of APIs using these tools.
Feel free to use this repository to learn more about Testcontainers, which I consider to be the more powerful thing in this boilerplate. If you want to know more about the template, keep reading. Want to jump directly to the code? Feel free to do so!
What are these Testcontainers and why should I use them?
The chef's kiss of this template is the usage of Testcontainers to make integration tests in the application's handlers.
This will help me in the future when building a more complex structure using another services like Kafka or Redis.
The best thing about using Testcontainers is to able to make integration tests and maintain your codebase fully testable if you use it correctly.
What were your difficulties when developing this project?
The most painful one was that I was not able to make Air work well with Docker: for some reason, my docker-compose file was not making the live-reload work correctly.
Still, the project works fine running go run main.go
or install Air and run with air
natively.
I have also tried to follow a dependency injection pattern in this boilerplate, which helped a lot when creating the tests for the handlers.
Conclusion
We could indeed use other tools to make tests. You could mock the repositories or find an library that helps to do them. I honestly do not know if there is a mongodb-memory-server
like or equivalent to Go, but would be very happy to know if that is the case.
If you are interested in using Testcontainers in Go and do not want to do all the groundwork, feel free to use my template that is available here: gogimon.
Top comments (0)