Directory Layout
The cmd/ Directory
Contains the entry point(s) for your application.cmd/api/main.go
- Loads configuration
- Creates the application
- Starts the HTTP server
- Keeps
main()minimal - Makes it easy to add more commands (workers, migrations)
- Application logic is testable without
main()
The app/ Directory
Contains application setup and configuration.app/api/app.go
- Defines the
Appstruct that holds configuration and router - Provides a
New()constructor that sets everything up - Exposes a
Listen()method to start the server
| Part | Purpose |
|---|---|
App struct | Groups related state together |
New() | Constructor pattern for setup |
routes() | Called in constructor to register routes |
Listen() | Thin wrapper around mizu’s Listen |
app/api/config.go
- Defines configuration structure
- Loads values from environment variables
- Provides sensible defaults
app/api/routes.go
- Registers all routes in one place
- Imports handlers from feature packages
- Makes route structure visible at a glance
The feature/ Directory
Contains your business logic, organized by domain.feature/health/http.go
- Handlers can accept dependencies (database, logger)
- Clean separation between setup and execution
- Easy to test
feature/hello/http.go
feature/echo/http.go
- Reads JSON from request body
- Returns the same JSON back
- Useful for testing
feature/users/http.go
- Type definitions live with their handlers
- Mock data for the example (replace with database)
- Clean API boundary
Adding a New Feature
Let’s add aproducts feature:
1. Create the Package
2. Create the Handler
Createfeature/products/http.go:
3. Register Routes
Editapp/api/routes.go:
4. Test It
File Naming Conventions
| File | Purpose |
|---|---|
http.go | HTTP handlers |
service.go | Business logic (if needed) |
store.go | Database operations (if needed) |
types.go | Type definitions (if many) |