Directory Structure
Understanding the Goose project structure helps you organize your code effectively.
API Application Structure
myapi/
โโโ .env # Environment variables
โโโ .gitignore # Git ignore rules
โโโ go.mod # Go module definition
โโโ go.sum # Go dependencies checksum
โโโ main.go # Application entry point
โโโ config/ # Configuration files (optional)
โ โโโ app.yaml
โ โโโ database.yaml
โโโ app/ # Main application module
โ โโโ app.module.go # Module definition
โ โโโ app.controller.go # Request handlers
โ โโโ app.service.go # Business logic
โ โโโ app.routes.go # Route definitions
โ โโโ app.dtos.go # Data transfer objects
โ โโโ jobs/ # Background jobs (optional)
โ โ โโโ sample.job.go
โ โโโ queries/ # Database queries (optional)
โ โโโ sample.query.go
โโโ modules/ # Additional modules
โโโ users/
โโโ users.module.go
โโโ users.controller.go
โโโ users.service.go
โโโ users.routes.go
โโโ users.dtos.go
Web Application Structure
myweb/
โโโ .env
โโโ go.mod
โโโ main.go
โโโ app/
โ โโโ app.module.go
โ โโโ app.controller.go
โ โโโ app.service.go
โ โโโ app.routes.go
โ โโโ app.dtos.go
โ โโโ templates/ # HTML templates
โ โโโ base/
โ โ โโโ layout.html
โ โโโ pages/
โ โ โโโ home.html
โ โโโ partials/
โ โโโ header.html
โ โโโ footer.html
โโโ static/ # Static assets
โ โโโ css/
โ โโโ js/
โ โโโ images/
โโโ modules/
CLI Application Structure
mycli/
โโโ .env
โโโ go.mod
โโโ main.go
โโโ app/
โโโ app.module.go
โโโ app.controller.go
โโโ app.service.go
โโโ app.routes.go # Command definitions
โโโ app.dtos.go
Multi-Platform Structure
mymulti/
โโโ .env
โโโ go.mod
โโโ main.go
โโโ app/
โโโ api/ # API platform
โ โโโ api.module.go
โ โโโ api.controller.go
โ โโโ api.routes.go
โ โโโ api.service.go
โโโ web/ # Web platform
โ โโโ web.module.go
โ โโโ web.controller.go
โ โโโ web.routes.go
โ โโโ web.service.go
โ โโโ templates/
โโโ cli/ # CLI platform
โ โโโ cli.module.go
โ โโโ cli.controller.go
โ โโโ cli.routes.go
โ โโโ cli.service.go
โโโ shared/ # Shared components
โโโ shared.module.go
โโโ shared.service.go
โโโ entities/
Module Structure
Plain Module
A basic module for simple functionality:
users/
โโโ users.module.go # Module definition
โโโ users.controller.go # Request handlers
โโโ users.service.go # Business logic
โโโ users.routes.go # Route definitions
โโโ users.dtos.go # Data transfer objects
Resource Module
A module with database entity:
products/
โโโ products.module.go
โโโ products.controller.go
โโโ products.service.go
โโโ products.routes.go
โโโ products.dtos.go
โโโ products.entity.go # Database entity
โโโ migrations/ # Database migrations
โ โโโ 001_create_products.go
โโโ seeds/ # Seed data
โโโ products.seed.go
File Naming Conventions
| File | Purpose | Example |
|---|---|---|
*.module.go |
Module definition | app.module.go |
*.controller.go |
Request handlers | app.controller.go |
*.service.go |
Business logic | app.service.go |
*.routes.go |
Route definitions | app.routes.go |
*.dtos.go |
Data transfer objects | app.dtos.go |
*.entity.go |
Database entities | user.entity.go |
*.middleware.go |
Middleware | auth.middleware.go |
*.job.go |
Background jobs | email.job.go |
Key Files Explained
main.go
The entry point of your application:
package main
import (
"myapp/app"
"github.com/awesome-goose/goose"
"github.com/awesome-goose/goose/platforms/api"
)
func main() {
platform := api.NewPlatform()
module := &app.AppModule{}
stop, err := goose.Start(goose.API(platform, module, nil))
if err != nil {
panic(err)
}
defer stop()
}
app.module.go
Defines the module structure:
package app
import "github.com/awesome-goose/goose/types"
type AppModule struct{}
func (m *AppModule) Imports() []types.Module {
return []types.Module{
// Import other modules
}
}
func (m *AppModule) Exports() []any {
return []any{
// Export services for other modules
}
}
func (m *AppModule) Declarations() []any {
return []any{
&AppController{},
&AppService{},
}
}
.env
Environment configuration:
APP_NAME=myapp
APP_ENV=development
HOST=localhost
PORT=8080
Organizing Large Applications
For large applications, organize by feature:
app/
โโโ app.module.go # Root module
โโโ auth/ # Authentication feature
โ โโโ auth.module.go
โ โโโ auth.controller.go
โ โโโ auth.service.go
โ โโโ auth.middleware.go
โโโ users/ # Users feature
โโโ products/ # Products feature
โโโ orders/ # Orders feature
โโโ shared/ # Shared utilities
โโโ shared.module.go
โโโ validators/
โโโ helpers/
Next Steps
- Modules - Deep dive into modules
- Controllers - Controller patterns
- Services - Service layer