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
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
Best Practices
Keep Handlers Thin
Use Dependency Injection
Group Related Routes
Next Steps
Tutorial
Build a complete API from scratch
Contract Template
See an even more structured approach