go-standard-layout
Reading some articles for designing go standard layout. These are the guidelines that I use:
- Root package is for domain types
- Group subpackages by dependency
- Make dependencies explicit!
- Main package ties together dependencies
- Loggers are dependencies!
- Use the “underscore test” package
- Use a shared mock subpackage
- Using table-driven-test style for testing
- Provide database integration test
testdata
folder name for containing test fixtures- Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values
- The implementing package should return concrete (usually pointer or struct) types
About the project
This is a small app that consist of 2 entities, User and Article. 1 user can have more than 1 articles. For both entities, there are some sample endpoints provided. I wrap the router and provides basic monitoring using prometheus. I use dep for depedency management. There is simple opentracing implementation in this app. This app is also provided with unit test and parallel database integration test.
About the structure
The files in the root package contains domain logic (business logic) of the app. cmd
contains entrypoint of the app / command. script
contains shell script that can help to automate the build process, etc. Currently, script
contains script for running test with code coverage preview. The rest of folders are the dependencies of the app. The data layer is at mysql
package. You can find list of endpoints in router
package. HTTP handler is located in handler
package inside server
folder.
References
- https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1
- https://peter.bourgon.org/go-best-practices-2016
- https://medium.com/@benbjohnson/structuring-tests-in-go-46ddee7a25c
- https://www.youtube.com/watch?v=yszygk1cpEc
- https://dave.cheney.net/2016/05/10/test-fixtures-in-go
- https://github.com/golang/go/wiki/CodeReviewComments#interfaces