
structuring golang projects (uncompleted)
Structuring Golang Projects
Who never ask yourself about what is the best way to create a new project? Should I use the Hexagonal, DDD, Clean and these others fancy architectures? Go has a different style, but when you are a newcomer in the language, probably you donβt had the enough time to consume and read all the good repositories and resources to drive your decisions. This post is a collection of my thoughts and experiences about how to structure Go projects. With name conventions, package organization, and some tips to make your codebase more readable and maintainable.
Dilemmas

- Should I put everything together?
- Start small and grow?
- What is the βGo wayβ?
What is non-negotiable?
- Consistency across the codebase
- Easy to change, loosely coupled
- Easy to test, that implies decoupled package with clear duties and goals
- Design reflect how the software works
Grouping
Listing some feasible approaches to organize your golang codebase.
Flat
.
βββ cmd
β βββ main.go
βββ flat
βββ disk.go
βββ http.go
βββ inmem.go
βββ lamps.go
βββ output.txt
βββ processes.go
At least for me, in a first glance, seems a good approach. Everything is together, easy to find, easy to change. But, obviously, when the projects grow, this approach will become a mess.
By Function/Layer
By Context
.
βββ Dockerfile
βββ Makefile
βββ cmd
β βββ appname
β βββ main.go
βββ compose.yaml
βββ domain1.go
βββ domain2.go
βββ domain3.go
βββ firestore
β βββ domain1.go
β βββ firestore.go
βββ http
β βββ docs
β β βββ swagger.yaml
β βββ domain1.go
β βββ http.go
βββ mock
β βββ domain1.go
βββ output.txt
βββ postgres
β βββ domain1.go
β βββ migrations
β β βββ migration1-down.sql
β β βββ migration1-up.sql
β βββ postgres.go
βββ pubsub
βββ domain3.go
βββ pubsub.go
Package organization
This was the best definition of how to organize packages in the golang style without screwing up the codebase. Itβs a 2 definition rule that shape the whole codebase.
Domain Types
The domain types are the ones that represent the business logic of the application.
Service
The service packages are the ones that will operate on the domain types.