关于
This skill scaffolds new Go projects with appropriate directory structures and conventions based on project size. It helps developers choose between flat layouts or structured approaches with cmd/ and internal/ directories, covering module naming and main package wiring. Use it when starting a new Go module or service, but not for reviewing existing architectures or detailed dependency injection.
快速安装
Claude Code
推荐npx skills add eduardo-sl/go-agent-skills -a claude-code/plugin add https://github.com/eduardo-sl/go-agent-skillsgit clone https://github.com/eduardo-sl/go-agent-skills.git ~/.claude/skills/go-project-layout在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Go Project Layout
Structure follows size. The biggest layout mistake in Go is copying a microservice skeleton for a 500-line tool — or growing a 50-package service inside a flat directory. Match the layout to the project.
1. Pick the Layout by Project Size
| Project | Layout |
|---|---|
| Small tool, single binary, <5 files | Flat: everything in package main at the root |
| Library for others to import | Root package named after the module, internal/ for helpers |
| Service with one binary | cmd/<name>/main.go + internal/ packages |
| Multiple binaries sharing code | cmd/<name1>/, cmd/<name2>/ + internal/ |
Never start with empty pkg/, api/, docs/, build/ directories
"for later". Add structure when the code demands it, not before.
2. Module Naming
# ✅ Good — repository path, lowercase
go mod init github.com/acme/payment-service
# ❌ Bad — not fetchable, uppercase, or vanity without DNS
go mod init PaymentService
go mod init payment_service
The last path element should match what users will see: for a library, it becomes the default import name.
3. Service Layout (the default for APIs and workers)
payment-service/
├── cmd/
│ └── payment-api/
│ └── main.go # flag/env parsing, wiring, Run() — nothing else
├── internal/
│ ├── domain/ # core types, business rules; zero external deps
│ ├── service/ # use cases orchestrating domain + stores
│ ├── store/ # data access implementations (postgres/, redis/)
│ ├── handler/ # HTTP/gRPC adapters
│ └── config/ # config loading and validation
├── migrations/ # if the service owns a database
├── go.mod
├── Makefile
└── README.md
Rules:
internal/by default — the compiler enforces that nobody outside the module imports it. Promote to a public package only on demand.pkg/only when external consumers exist AND the module also has private code. When in doubt, don't create it.- Dependencies point inward:
handler → service → domain ← store.domainimports neitherstorenorhandler.
4. Thin main, Runnable Run
Keep main.go to wiring plus a delegating call, so the app is testable:
func main() {
if err := run(context.Background(), os.Args[1:], os.Getenv); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(ctx context.Context, args []string, getenv func(string) string) error {
cfg, err := config.Load(getenv)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
db, err := store.Open(ctx, cfg.DatabaseURL)
if err != nil {
return fmt.Errorf("open db: %w", err)
}
defer db.Close()
svc := service.New(store.NewUserRepo(db))
srv := handler.NewServer(cfg.Addr, svc)
return srv.ListenAndServe(ctx)
}
os.Exitappears exactly once, inmain.runtakes its dependencies (args,getenv) so tests can call it.- No
init()functions for wiring — explicit construction order only.
5. Library Layout
retry/
├── retry.go # package retry — the API, in the root
├── retry_test.go
├── backoff.go # same package, split by topic
├── internal/
│ └── clock/ # implementation details users must not import
├── examples_test.go # Example* functions shown in godoc
└── go.mod
- The root directory IS the package. No
src/, nolib/. - One package per concept. Resist
util,common,helpers— name packages after what they provide (retry,clock,httpsign).
6. Naming Rules for Directories and Packages
- Package name == directory name, short, lowercase, no underscores:
store/postgres, notstore/postgres_impl. - Don't stutter:
payment.Service, notpayment.PaymentService. - Binary names in
cmd/are user-facing:cmd/payment-api, hyphenated is fine (directory only holds packagemain).
7. Files That Belong at the Root
go.mod,go.sum,README.md,LICENSE,Makefile,.golangci.yml,Dockerfile(single-binary projects).- Do NOT create:
src/(un-idiomatic),vendor/(unless the team explicitly vendors), one-file packages liketypes/ormodels/that become dumping grounds.
Scaffolding Procedure
- Ask/decide: tool, library, or service? How many binaries?
go mod init <repo-path>.- Create only the directories the first feature needs.
- Write
main.gowith the thin-main pattern above. - Add
Makefiletargets:build,test,lint. - Verify:
go build ./...andgo vet ./...pass on the skeleton.
Verification Checklist
- Layout matches project size — no empty scaffolding directories
- Module path is the fetchable repository path
- All non-public packages live under
internal/ main.gois thin: parse, wire, callrun, exitos.Exitonly inmain; no wiring ininit()- Dependencies flow inward;
domainhas zero infrastructure imports - No
util/common/helpers/modelsgrab-bag packages - Package names match directories, lowercase, no stutter
go build ./...passes on the fresh skeleton
GitHub 仓库
常见问题
什么是 go-project-layout Skill?
go-project-layout 是一个 Claude Skill,作者为 eduardo-sl。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 go-project-layout 相关的任务。
如何安装 go-project-layout?
使用本页的安装命令:将 go-project-layout 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。
go-project-layout 属于哪个分类?
go-project-layout 属于元分类。
go-project-layout 可以免费使用吗?
可以。go-project-layout 已收录在 AIMCP,可免费安装。
相关推荐技能
Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。
这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。
该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。
SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。
