A template server for go projects using Gin
- Click the
Use this templatebutton to create a new repository using this template - Clone the new repository
- Delete the
go.sumfile - Use
go mod init <project name>to create a newgo.modfile - Run
go mod tidyto download the required dependencies
-
All the API code is in the
internalpackagehandlerscontains the API handlers which will define the routes of the APIdtocontains all the model structs for the APIcontrollerscontains the business logic for the APIappcontains the main function to start the server
-
The API test code will be written inside the
internal/controllersdir only and will follow table driven tests approach -
pkgcontains the common code which can be used across multiple projects. We can add directories in like mongo, repository, etc. -
cmdcontains the main function to start the server -
utilcontains the utility functions which can be used across the project. -
Controllers and Handlers will not be responsible for sending of data to the client. They will only be responsible for processing the data and sending it to the client.
-
The response will be sent by the
util.HandlerWrapperfunction. This makes the code more testable and maintainable.