A collection of things I consider to be best practices in test-driven go development. This applies mainly to writing applications in Go, in particular JSON APIs, but can be extrapolated to command line tools, libraries and framework too.
This was inspired by Can Berk Guder's iOS best practices repo.
Some of the practices and principles I tried to demonstrate in this sample project are:
- Test-driven development using Ginkgo and Gomega
- Type-safe test doubles using Counterfeiter
- Single responsibility principle
- Composition over inheritance
- Dependency injection using hand-rolled constructor functions
- Declaring interface for dependencies near the consumer
- Vendored dependencies with a vendoring tool you enjoy using
- Responsible usage of concurrency
- Minimal integration tests with Gomega's gexec package
- Avoid named returns in functions whenever possible
- main.go
- gvt
- webserver with /pizza/order route
- order pizza use case
- pizza repository
- wire up /pizza/order to usecase
- minimal gexec test
- gexec test asserts we can make a single http request
- middleware (composition)
- logging
- stats ?
- configuration (port, log server, stats server)
- panic handler (middleware?)
- find a better name for "domain" package (h/t to Dave Cheney)
#notes
- I prefer using
gvt
to manage my dependencies, but you may prefergodep
, or just doing it by hand. So long as you keep your dependencies tracked, make it easy to setup new development environments and bump dependencies regularly, you should be fine.