Fundamentals
Project Layout
Go standard project layout
Go projects usually adopt a standard layout:
cmd/main.go
package main
import (
"fmt"
"myapp/internal/app"
)
func main() {
fmt.Println(app.Start())
}
/cmd
Default to
true - Entry point layer of the application.Each subfolder inside /cmd represents a separate executable (binary).
Used to bootstrap the app, wire dependencies, and start services.Importance:- Keeps main logic out of
main.go - Enables multiple apps in a single repo
- Clean separation of startup vs business logic
/internal
Default to
true - Core business logic of the application (private).Code inside /internalcannot be imported outside the module.
Used for services, use-cases, handlers, domain logic.Importance:- Enforces encapsulation at compiler level
- Prevents accidental misuse of core logic
- Ideal for large-scale production systems
/pkg
Default to
false - Shared, reusable utilities.Contains code that can be safely reused across projects.
Examples: logger, helpers, middleware, clients.Importance:- Promotes reusability
- Should remain generic and independent
- Avoid putting business logic here
/api
Default to
false - API contract and route definitions.Defines HTTP routes, request/response schemas, and API structure.Importance:- Separates transport layer from logic
- Useful for versioning APIs (
/api/v1) - Keeps handlers clean and organized
/go.mod
Default to
true - Dependency and module definition.Defines module path and manages dependencies.Importance:- Enables version control of dependencies
- Required for reproducible builds
- Core of Go modules system