golang-documentation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePersona: You are a Go technical writer and API designer. You treat documentation as a first-class deliverable — accurate, example-driven, and written for the reader who has never seen this codebase before.
Modes:
- Write mode — generating or filling in missing documentation (doc comments, README, CONTRIBUTING, CHANGELOG, llms.txt). Work sequentially through the checklist in Step 2, or parallelize across packages/files using sub-agents.
- Review mode — auditing existing documentation for completeness, accuracy, and style. Use up to 5 parallel sub-agents: one per documentation layer (doc comments, README, CONTRIBUTING, CHANGELOG, library-specific extras).
Community default. A company skill that explicitly supersedesskill takes precedence.samber/cc-skills-golang@golang-documentation
角色定位: 你是一名Go技术文档工程师和API设计师。你将文档视为一等交付成果——内容准确、以示例为导向,面向从未接触过该代码库的读者编写。
模式:
- 编写模式 —— 生成或补全缺失的文档(文档注释、README、CONTRIBUTING、CHANGELOG、llms.txt)。可以按步骤2的检查清单顺序处理,也可以使用子Agent并行处理多个包/文件。
- 审阅模式 —— 审核现有文档的完整性、准确性和风格。最多可使用5个并行子Agent:每个子Agent负责一个文档层级(文档注释、README、CONTRIBUTING、CHANGELOG、类库专属附加文档)。
社区默认规则:若公司内部有明确替代的skill,以公司skill为准。samber/cc-skills-golang@golang-documentation
Go Documentation
Go文档编写指南
Write documentation that serves both humans and AI agents. Good documentation makes code discoverable, understandable, and maintainable.
编写同时服务于人类和AI Agent的文档。优质文档能让代码更易被发现、理解和维护。
Cross-References
交叉参考
See skill for naming conventions in doc comments. See skill for Example test functions. See skill for where documentation files belong.
samber/cc-skills-golang@golang-namingsamber/cc-skills-golang@golang-testingsamber/cc-skills-golang@golang-project-layout文档注释的命名规范请参考 skill。示例测试函数相关内容请参考 skill。文档文件的存放位置请参考 skill。
samber/cc-skills-golang@golang-namingsamber/cc-skills-golang@golang-testingsamber/cc-skills-golang@golang-project-layoutStep 1: Detect Project Type
步骤1:检测项目类型
Before documenting, determine the project type — it changes what documentation is needed:
Library — no package, meant to be imported by other projects:
main- Focus on godoc comments, functions, playground demos, pkg.go.dev rendering
ExampleXxx - See Library Documentation
Application/CLI — has package, directory, produces a binary or Docker image:
maincmd/- Focus on installation instructions, CLI help text, configuration docs
- See Application Documentation
Both apply: function comments, README, CONTRIBUTING, CHANGELOG.
Architecture docs: for complex projects, use directory and design description docs.
docs/在编写文档前,先确定项目类型——这会影响所需的文档内容:
类库 —— 无包,供其他项目导入使用:
main- 重点关注godoc注释、函数、Playground演示、pkg.go.dev渲染效果
ExampleXxx - 查看类库文档
应用/CLI —— 包含包、目录,生成二进制文件或Docker镜像:
maincmd/- 重点关注安装说明、CLI帮助文本、配置文档
- 查看应用文档
两者通用:函数注释、README、CONTRIBUTING、CHANGELOG。
架构文档:针对复杂项目,使用目录和设计描述文档。
docs/Step 2: Documentation Checklist
步骤2:文档检查清单
Every Go project needs these (ordered by priority):
| Item | Required | Library | Application |
|---|---|---|---|
| Doc comments on exported functions | Yes | Yes | Yes |
Package comment ( | Yes | Yes | Yes |
| README.md | Yes | Yes | Yes |
| LICENSE | Yes | Yes | Yes |
| Getting started / installation | Yes | Yes | Yes |
| Working code examples | Yes | Yes | Yes |
| CONTRIBUTING.md | Recommended | Yes | Yes |
| CHANGELOG.md or GitHub Releases | Recommended | Yes | Yes |
Example test functions ( | Recommended | Yes | No |
| Go Playground demos | Recommended | Yes | No |
| API docs (eg: OpenAPI) | If applicable | Maybe | Maybe |
| Documentation website | Large projects | Maybe | Maybe |
| llms.txt | Recommended | Yes | Yes |
A private project might not need documentation website, llms.txt, Go Playground demos...
所有Go项目都需要以下文档(按优先级排序):
| 项 | 是否必填 | 类库 | 应用 |
|---|---|---|---|
| 导出函数的文档注释 | 是 | 是 | 是 |
包注释( | 是 | 是 | 是 |
| README.md | 是 | 是 | 是 |
| LICENSE | 是 | 是 | 是 |
| 快速开始 / 安装说明 | 是 | 是 | 是 |
| 可运行代码示例 | 是 | 是 | 是 |
| CONTRIBUTING.md | 推荐 | 是 | 是 |
| CHANGELOG.md 或 GitHub Releases | 推荐 | 是 | 是 |
示例测试函数( | 推荐 | 是 | 否 |
| Go Playground 演示 | 推荐 | 是 | 否 |
| API文档(如:OpenAPI) | 按需提供 | 可选 | 可选 |
| 文档站点 | 大型项目 | 可选 | 可选 |
| llms.txt | 推荐 | 是 | 是 |
私有项目可无需文档站点、llms.txt、Go Playground演示...
Parallelizing Documentation Work
文档工作并行处理
When documenting a large codebase with many packages, use up to 5 parallel sub-agents (via the Agent tool) for independent tasks:
- Assign each sub-agent to verify and fix doc comments in a different set of packages
- Generate test functions for multiple packages simultaneously
ExampleXxx - Generate project docs in parallel: one sub-agent per file (README, CONTRIBUTING, CHANGELOG, llms.txt)
当为包含多个包的大型代码库编写文档时,最多可使用5个并行子Agent(通过Agent工具)处理独立任务:
- 为每个子Agent分配不同包集合,负责验证和修复文档注释
- 同时为多个包生成测试函数
ExampleXxx - 并行生成项目文档:每个子Agent负责一个文件(README、CONTRIBUTING、CHANGELOG、llms.txt)
Step 3: Function & Method Doc Comments
步骤3:函数与方法文档注释
Every exported function and method MUST have a doc comment. Document complex internal functions too. Skip test functions.
The comment starts with the function name and a verb phrase. Focus on why and when, not restating what the code already shows. The code tells you what happens — the comment should explain why it exists, when to use it, what constraints apply, and what can go wrong. Include parameters, return values, error cases, and a usage example:
go
// CalculateDiscount computes the final price after applying tiered discounts.
// Discounts are applied progressively based on order quantity: each tier unlocks
// additional percentage reduction. Returns an error if the quantity is invalid or
// if the base price would result in a negative value after discount application.
//
// Parameters:
// - basePrice: The original price before any discounts (must be non-negative)
// - quantity: The number of units ordered (must be positive)
// - tiers: A slice of discount tiers sorted by minimum quantity threshold
//
// Returns the final discounted price rounded to 2 decimal places.
// Returns ErrInvalidPrice if basePrice is negative.
// Returns ErrInvalidQuantity if quantity is zero or negative.
//
// Play: https://go.dev/play/p/abc123XYZ
//
// Example:
//
// tiers := []DiscountTier{
// {MinQuantity: 10, PercentOff: 5},
// {MinQuantity: 50, PercentOff: 15},
// {MinQuantity: 100, PercentOff: 25},
// }
// finalPrice, err := CalculateDiscount(100.00, 75, tiers)
// if err != nil {
// log.Fatalf("Discount calculation failed: %v", err)
// }
// log.Printf("Ordered 75 units at $100 each: final price = $%.2f", finalPrice)
func CalculateDiscount(basePrice float64, quantity int, tiers []DiscountTier) (float64, error) {
// implementation
}For the full comment format, deprecated markers, interface docs, and file-level comments, see Code Comments — how to document packages, functions, interfaces, and when to use markers and notes.
Deprecated:BUG:所有导出函数和方法必须有文档注释。复杂的内部函数也建议添加注释。测试函数可跳过。
注释需以函数名和动词短语开头。重点说明原因和使用场景,而非重复代码已体现的内容。代码会告诉你_发生了什么_——注释应解释_为什么存在_、何时使用、有哪些约束,以及_可能出现的问题_。需包含参数、返回值、错误场景和使用示例:
go
// CalculateDiscount computes the final price after applying tiered discounts.
// Discounts are applied progressively based on order quantity: each tier unlocks
// additional percentage reduction. Returns an error if the quantity is invalid or
// if the base price would result in a negative value after discount application.
//
// Parameters:
// - basePrice: The original price before any discounts (must be non-negative)
// - quantity: The number of units ordered (must be positive)
// - tiers: A slice of discount tiers sorted by minimum quantity threshold
//
// Returns the final discounted price rounded to 2 decimal places.
// Returns ErrInvalidPrice if basePrice is negative.
// Returns ErrInvalidQuantity if quantity is zero or negative.
//
// Play: https://go.dev/play/p/abc123XYZ
//
// Example:
//
// tiers := []DiscountTier{
// {MinQuantity: 10, PercentOff: 5},
// {MinQuantity: 50, PercentOff: 15},
// {MinQuantity: 100, PercentOff: 25},
// }
// finalPrice, err := CalculateDiscount(100.00, 75, tiers)
// if err != nil {
// log.Fatalf("Discount calculation failed: %v", err)
// }
// log.Printf("Ordered 75 units at $100 each: final price = $%.2f", finalPrice)
func CalculateDiscount(basePrice float64, quantity int, tiers []DiscountTier) (float64, error) {
// implementation
}关于完整注释格式、废弃标记、接口文档和文件级注释,请查看**代码注释**——包含如何为包、函数、接口编写注释,以及何时使用标记和说明。
Deprecated:BUG:Step 4: README Structure
步骤4:README结构
README SHOULD follow this exact section order. Copy the template from templates/README.md:
- Title — project name as
# heading - Badges — shields.io pictograms (Go version, license, CI, coverage, Go Report Card...)
- Summary — 1-2 sentences explaining what the project does
- Demo — code snippet, GIF, screenshot, or video showing the project in action
- Getting Started — installation + minimal working example
- Features / Specification — detailed feature list or specification (very long section)
- Contributing — link to CONTRIBUTING.md or inline if very short
- Contributors — thank contributors (badge or list)
- License — license name + link
Common badges for Go projects:
markdown
[](https://go.dev/) [](./LICENSE) [](https://github.com/{owner}/{repo}/actions) [](https://codecov.io/gh/{owner}/{repo}) [](https://goreportcard.com/report/github.com/{owner}/{repo}) [](https://pkg.go.dev/github.com/{owner}/{repo})For the full README guidance and application-specific sections, see Project Docs.
README应严格遵循以下章节顺序。可从templates/README.md复制模板:
- 标题 —— 项目名称,使用标题格式
# - 徽章 —— shields.io图标(Go版本、许可证、CI、覆盖率、Go Report Card...)
- 概述 —— 1-2句话说明项目功能
- 演示 —— 代码片段、GIF、截图或视频展示项目运行效果
- 快速开始 —— 安装说明 + 最简可运行示例
- 特性 / 规格 —— 详细特性列表或规格说明(较长章节)
- 贡献指南 —— 链接到CONTRIBUTING.md,若内容极短可直接内联
- 贡献者 —— 感谢贡献者(徽章或列表形式)
- 许可证 —— 许可证名称 + 链接
Go项目常用徽章:
markdown
[](https://go.dev/) [](./LICENSE) [](https://github.com/{owner}/{repo}/actions) [](https://codecov.io/gh/{owner}/{repo}) [](https://goreportcard.com/report/github.com/{owner}/{repo}) [](https://pkg.go.dev/github.com/{owner}/{repo})完整README指南及应用专属章节,请查看项目文档。
Step 5: CONTRIBUTING & Changelog
步骤5:CONTRIBUTING与变更日志
CONTRIBUTING.md — Help contributors get started in under 10 minutes. Include: prerequisites, clone, build, test, PR process. If setup takes longer than 10 minutes, then you should improve the process: add a Makefile, docker-compose, or devcontainer to simplify it. See Project Docs.
Changelog — Track changes using Keep a Changelog format or GitHub Releases. Copy the template from templates/CHANGELOG.md. See Project Docs.
CONTRIBUTING.md —— 帮助贡献者在10分钟内上手。需包含:前置依赖、克隆、构建、测试、PR流程。若搭建过程超过10分钟,应优化流程:添加Makefile、docker-compose或devcontainer简化操作。查看项目文档获取详情。
Step 6: Library-Specific Documentation
步骤6:类库专属文档
For Go libraries, add these on top of the basics:
- Go Playground demos — create runnable demos and link them in doc comments with . Use the go-playground MCP tool when available to create and share playground URLs.
// Play: https://go.dev/play/p/xxx - Example test functions — write in
func ExampleXxx()files. These are executable documentation verified by_test.go.go test - Generous code examples — include multiple examples in doc comments showing common use cases.
- godoc — your doc comments render on pkg.go.dev. Use locally to preview.
go doc - Documentation website — for large libraries, consider Docusaurus or MkDocs Material with sections: Getting Started, Tutorial, How-to Guides, Reference, Explanation.
- Register for discoverability — add to Context7, DeepWiki, OpenDeep, zRead. Even for private libraries.
See Library Documentation for details.
对于Go类库,在基础文档之外还需添加以下内容:
- Go Playground演示 —— 创建可运行演示,并在文档注释中通过链接。若有可用的go-playground MCP工具,可用于创建和分享Playground链接。
// Play: https://go.dev/play/p/xxx - 示例测试函数 —— 在文件中编写
_test.go。这是可执行文档,会通过func ExampleXxx()验证。go test - 丰富的代码示例 —— 在文档注释中添加多个示例,展示常见使用场景。
- godoc —— 文档注释会在pkg.go.dev上渲染。可使用本地命令预览。
go doc - 文档站点 —— 大型类库可考虑使用Docusaurus或MkDocs Material,包含章节:快速开始、教程、操作指南、参考、说明。
- 注册以提升可发现性 —— 添加到Context7、DeepWiki、OpenDeep、zRead。私有类库也可操作。
查看类库文档获取详情。
Step 7: Application-Specific Documentation
步骤7:应用专属文档
For Go applications/CLIs:
- Installation methods — pre-built binaries (GoReleaser), , Docker images, Homebrew...
go install - CLI help text — make comprehensive; it's the primary documentation
--help - Configuration docs — document all env vars, config files, CLI flags
See Application Documentation for details.
对于Go应用/CLI:
- 安装方式 —— 预构建二进制文件(GoReleaser)、、Docker镜像、Homebrew...
go install - CLI帮助文本 —— 确保内容全面;这是用户的主要参考文档
--help - 配置文档 —— 记录所有环境变量、配置文件、CLI参数
查看应用文档获取详情。
Step 8: API Documentation
步骤8:API文档
If your project exposes an API:
| API Style | Format | Tool |
|---|---|---|
| REST/HTTP | OpenAPI 3.x | swaggo/swag (auto-generate from annotations) |
| Event-driven | AsyncAPI | Manual or code-gen |
| gRPC | Protobuf | buf, grpc-gateway |
Prefer auto-generation from code annotations when possible. See Application Documentation for details.
若项目对外暴露API:
| API风格 | 格式 | 工具 |
|---|---|---|
| REST/HTTP | OpenAPI 3.x | swaggo/swag(通过注解自动生成) |
| 事件驱动 | AsyncAPI | 手动编写或代码生成 |
| gRPC | Protobuf | buf, grpc-gateway |
优先选择通过代码注解自动生成。查看应用文档获取详情。
Step 9: AI-Friendly Documentation
步骤9:AI友好型文档
Make your project consumable by AI agents:
- llms.txt — add a file at the repository root. Copy the template from templates/llms.txt. This file gives LLMs a structured overview of your project.
llms.txt - Structured formats — use OpenAPI, AsyncAPI, or protobuf for machine-readable API docs.
- Consistent doc comments — well-structured godoc comments are easily parsed by AI tools.
- Clarity — a clear, well-structured documentation helps AI agents understand your project quickly.
让项目可被AI Agent轻松使用:
- llms.txt —— 在仓库根目录添加文件。可从templates/llms.txt复制模板。该文件为LLM提供项目的结构化概述。
llms.txt - 结构化格式 —— 使用OpenAPI、AsyncAPI或Protobuf生成机器可读的API文档。
- 一致的文档注释 —— 结构清晰的godoc注释易被AI工具解析。
- 简洁清晰 —— 结构清晰的文档能帮助AI Agent快速理解项目。
Step 10: Delivery Documentation
步骤10:交付文档
Document how users get your project:
Libraries:
bash
go get github.com/{owner}/{repo}Applications:
bash
undefined记录用户获取项目的方式:
类库:
bash
go get github.com/{owner}/{repo}应用:
bash
undefinedPre-built binary
预构建二进制文件
curl -sSL https://github.com/{owner}/{repo}/releases/latest/download/{repo}-$(uname -s)-$(uname -m) -o /usr/local/bin/{repo}
curl -sSL https://github.com/{owner}/{repo}/releases/latest/download/{repo}-$(uname -s)-$(uname -m) -o /usr/local/bin/{repo}
From source
从源码安装
go install github.com/{owner}/{repo}@latest
go install github.com/{owner}/{repo}@latest
Docker
Docker
docker pull {registry}/{owner}/{repo}:latest
See [Project Docs](./references/project-docs.md#delivery) for Dockerfile best practices and Homebrew tap setup.docker pull {registry}/{owner}/{repo}:latest
查看[项目文档](./references/project-docs.md#delivery)获取Dockerfile最佳实践和Homebrew Tap配置方法。