Go Project Setup
Create clean, idiomatic Go project structures.
Core Mandates
- Standard Layout: Use the official Go layout (
cmd/,internal/). Never create apkg/directory. - Flat by Default: Start with a flat structure (files in root) for simple apps. Introduce
cmd/andinternal/only when needed for private packages or multiple binaries. - Modern Go: Set
go.modto the latest stable Go version (currently 1.24+).
Workflow
1. Scope
Ask the user if this is a simple tool, a library, or a multi-service repository.
2. Choose Template
Read and use the templates in the assets/ directory to establish patterns like graceful shutdown, run functions, and package separation.
- Simple CLI:
assets/cli-simple. Flat structure. - Cobra CLI:
assets/cli-cobra. For complex CLI tools. - Library:
assets/library. Package in root,internal/for hidden logic. - Service:
assets/webservice. Entry point incmd/app-name/main.gousing a run function, private logic ininternal/. - MCP Server:
assets/mcp-server. - Game:
assets/game. Uses Ebitengine.
3. Initialize
- Create Directory:
mkdir my-app - Init Module:
go mod init github.com/user/my-app - Linting: (Optional) Initialize
.golangci.ymlif style enforcement is required. - Apply Template: Copy files from the template and update module names.
- Verify: Run
go mod tidyandgo build ./....
References
references/project_layout.md: Official Go Module Layout guide.
微信扫一扫