GoLang APIs: A skeleton for your future projects
In this series of articles, I will break down my ultimate template for developing production-grade APIs, from project structure to full functionality, explaining the why and the how of every step.

Prerequisites
- Basic knowledge of GoLang is required as I will not be covering the basics. (Golang version 1.17)
- IDE of your choice, VSCode is my preference
Introduction
After developing several production-grade APIs for my company, I have found that having a well-structured skeleton reduces delivery time exponentially. This matters when delivery dates are rushed, business requirements are changed or delay, or when simply the team needs to have a quick but healthy delivery cycle. This part will be focused on project structure, basic functionality, and important dependencies. Code can be found here
What Do We Aim For?
When developing APIs, the main points that we focused on are performance and accessibility, and these include the basic response time, the query type and how much data are we going to be exposing.
Project Structure
I tend to follow the golang standard project layout when building any GoLang service and it really pays off as it keep everything organized.

This is an extended version on how the API would look like.
api/v1hosts all HTTP related files, it is a good practice to group and version the code. Under this you’ll find the router and handlers folder for the server logic.buildcontains all files related to deployment and image building such as docker file, helm charts, CICD, etccmd/<project-name>will keep our main.go file, i keep this file relatively small as a good practice.internalfolder will have all the different logic packages such as worker for business logic, env variables set up, etc.testingortestis simple for testing files, I keep the name of the file related to the type of testing such as unit_test.go or bench_test.go
Server Set Up
Our server logic would be the following, the server will be served in the main.go file, the initialization of the router (route mounting, middleware) will be in the router.go, and the handling definition in handler.go. We start with the router implementation, follow by handler definition and finally we implement the serve function in main.
We create a Initialize() function to set up our chi router, middleware and mounting the respecting handler routes. In terms of middleware, these are my most common ones. RedirectSlashes is meant to help trailing slashes get dropped so it does not interfere when getting parameters. I also added the CORS implementation for chi routers, most companies need to have CORS headers activated to receive cross domain requests. We use an anonymous function to implement the Mount() function to group routes under the same version/implementation.






