myapp/ ├── cmd/ # Entry points (main packages) │ └── myapp/ # Main app entry (main.go) ├── internal/ # Private app code │ ├── config/ # Configuration handling │ ├── handler/ # HTTP handlers (controllers) │ ├── service/ # Business logic │ ├── repository/ # Data access logic │ └── model/ # Domain models ├── pkg/ # Reusable packages (public) │ └── utils/ # Utilities used across the app ├── web/ # Frontend assets (HTML, JS, CSS) │ ├── static/ # Static files │ └── templates/ # HTML templates ├── migrations/ # DB schema and migration files ├── go.mod # Go module definition ├── go.sum # Go module checksums └── README.md When building a Go (Golang) web application, it's important to organize your folder structure in a way that supports scalability, maintainability, and testability. There’s no one-size-fits-all, but here is a commonly used idiomatic folder structure for a Go web app:
myapp/
├── cmd/ # Entry points (main packages)
│ └── myapp/ # Main app entry (main.go)
├── internal/ # Private app code
│ ├── config/ # Configuration handling
│ ├── handler/ # HTTP handlers (controllers)
│ ├── service/ # Business logic
│ ├── repository/ # Data access logic
│ └── model/ # Domain models
├── pkg/ # Reusable packages (public)
│ └── utils/ # Utilities used across the app
├── web/ # Frontend assets (HTML, JS, CSS)
│ ├── static/ # Static files
│ └── templates/ # HTML templates
├── migrations/ # DB schema and migration files
├── go.mod # Go module definition
├── go.sum # Go module checksums
└── README.md
-
cmd/myapp/main.goEntry point for your application. Keeps the root directory clean when you have multiple binaries. -
internal/Code here is private to the module. This is where your core logic resides:handler/handles HTTP routing and responses.service/contains business logic.repository/manages database interactions.model/defines your data structures.
-
pkg/Code intended to be imported by other apps (e.g., utilities or shared libraries). -
web/Stores frontend files served by the app. -
migrations/Keeps SQL files or Go-based migration scripts (for tools likegolang-migrateorgoose).