golang-documentation by samber/cc-skills-golang
npx skills add https://github.com/samber/cc-skills-golang --skill golang-documentation角色设定: 你是一名 Go 技术文档作者和 API 设计师。你将文档视为一等交付物——准确、示例驱动,并为从未见过此代码库的读者而写。
模式:
社区默认值。 明确取代
samber/cc-skills-golang@golang-documentation技能的团队技能具有优先权。
编写服务于人类和 AI 代理的文档。好的文档使代码易于发现、理解和维护。
关于文档注释中的命名约定,请参见 samber/cc-skills-golang@golang-naming 技能。关于示例测试函数,请参见 samber/cc-skills-golang@golang-testing 技能。关于文档文件的存放位置,请参见 samber/cc-skills-golang@golang-project-layout 技能。
在编写文档之前,确定项目类型——它决定了需要哪些文档:
—— 没有 包,旨在被其他项目导入:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
mainExampleXxx 函数、Go Playground 演示、pkg.go.dev 渲染应用程序/CLI —— 有 main 包、cmd/ 目录,生成二进制文件或 Docker 镜像:
两者都适用:函数注释、README、CONTRIBUTING、CHANGELOG。
架构文档:对于复杂项目,使用 docs/ 目录和设计描述文档。
每个 Go 项目都需要这些(按优先级排序):
| 项目 | 必需 | 库 | 应用程序 |
|---|---|---|---|
| 导出函数的文档注释 | 是 | 是 | 是 |
包注释 (// Package foo...) —— 必须存在 | 是 | 是 | 是 |
| README.md | 是 | 是 | 是 |
| LICENSE | 是 | 是 | 是 |
| 入门指南 / 安装说明 | 是 | 是 | 是 |
| 可运行的代码示例 | 是 | 是 | 是 |
| CONTRIBUTING.md | 推荐 | 是 | 是 |
| CHANGELOG.md 或 GitHub Releases | 推荐 | 是 | 是 |
示例测试函数 (ExampleXxx) | 推荐 | 是 | 否 |
| Go Playground 演示 | 推荐 | 是 | 否 |
| API 文档(例如:OpenAPI) | 如适用 | 可能 | 可能 |
| 文档网站 | 大型项目 | 可能 | 可能 |
| llms.txt | 推荐 | 是 | 是 |
私有项目可能不需要文档网站、llms.txt、Go Playground 演示...
当为包含许多包的大型代码库编写文档时,使用最多 5 个并行子代理(通过 Agent 工具)来处理独立任务:
ExampleXxx 测试函数每个导出的函数和方法必须有文档注释。复杂的内部函数也应记录。测试函数可以跳过。
注释以函数名和动词短语开头。专注于为什么和何时,而不是重复代码已经显示的内容。代码告诉你发生了什么——注释应该解释为什么它存在、何时使用它、有哪些约束适用以及可能出现什么问题。包括参数、返回值、错误情况和用法示例:
// CalculateDiscount 计算应用分级折扣后的最终价格。
// 折扣根据订单数量逐步应用:每个层级解锁额外的百分比减免。
// 如果数量无效或基础价格在应用折扣后会导致负值,则返回错误。
//
// 参数:
// - basePrice: 任何折扣前的原始价格(必须为非负数)
// - quantity: 订购的单位数量(必须为正数)
// - tiers: 按最小数量阈值排序的折扣层级切片
//
// 返回四舍五入到 2 位小数的最终折扣价格。
// 如果 basePrice 为负数,返回 ErrInvalidPrice。
// 如果 quantity 为零或负数,返回 ErrInvalidQuantity。
//
// Play: https://go.dev/play/p/abc123XYZ
//
// 示例:
//
// 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("折扣计算失败: %v", err)
// }
// log.Printf("订购 75 个单位,单价 $100:最终价格 = $%.2f", finalPrice)
func CalculateDiscount(basePrice float64, quantity int, tiers []DiscountTier) (float64, error) {
// 实现
}
关于完整的注释格式、弃用标记、接口文档和文件级注释,请参见 代码注释 —— 如何记录包、函数、接口,以及何时使用 Deprecated: 标记和 BUG: 注释。
README 应该 遵循这个确切的章节顺序。从 templates/README.md 复制模板:
# 标题Go 项目的常用徽章:
[](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 指导和应用程序特定的章节,请参见 项目文档。
CONTRIBUTING.md —— 帮助贡献者在 10 分钟内上手。包括:先决条件、克隆、构建、测试、PR 流程。如果设置时间超过 10 分钟,那么你应该改进流程:添加 Makefile、docker-compose 或 devcontainer 来简化它。参见 项目文档。
更新日志 —— 使用 Keep a Changelog 格式或 GitHub Releases 跟踪变更。从 templates/CHANGELOG.md 复制模板。参见 项目文档。
对于 Go 库,在基础之上添加以下内容:
// Play: https://go.dev/play/p/xxx 链接它们。当可用时,使用 go-playground MCP 工具来创建和分享 playground URL。_test.go 文件中编写 func ExampleXxx()。这些是由 go test 验证的可执行文档。go doc 预览。详情请参见 库文档。
对于 Go 应用程序/CLI:
go install、Docker 镜像、Homebrew...--help 全面;这是主要的文档详情请参见 应用程序文档。
如果你的项目暴露了一个 API:
| API 风格 | 格式 | 工具 |
|---|---|---|
| REST/HTTP | OpenAPI 3.x | swaggo/swag(从注解自动生成) |
| 事件驱动 | AsyncAPI | 手动或代码生成 |
| gRPC | Protobuf | buf, grpc-gateway |
尽可能优先使用从代码注解自动生成。详情请参见 应用程序文档。
使你的项目能够被 AI 代理消费:
llms.txt 文件。从 templates/llms.txt 复制模板。该文件为 LLMs 提供了项目的结构化概览。记录用户如何获取你的项目:
库:
go get github.com/{owner}/{repo}
应用程序:
# 预构建的二进制文件
curl -sSL https://github.com/{owner}/{repo}/releases/latest/download/{repo}-$(uname -s)-$(uname -m) -o /usr/local/bin/{repo}
# 从源码安装
go install github.com/{owner}/{repo}@latest
# Docker
docker pull {registry}/{owner}/{repo}:latest
关于 Dockerfile 最佳实践和 Homebrew tap 设置,请参见 项目文档。
每周安装次数
101
仓库
GitHub 星标数
184
首次出现
3 天前
安全审计
安装于
opencode82
codex81
gemini-cli81
cursor81
kimi-cli80
github-copilot80
Persona: 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:
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-documentationskill takes precedence.
Write documentation that serves both humans and AI agents. Good documentation makes code discoverable, understandable, and maintainable.
See samber/cc-skills-golang@golang-naming skill for naming conventions in doc comments. See samber/cc-skills-golang@golang-testing skill for Example test functions. See samber/cc-skills-golang@golang-project-layout skill for where documentation files belong.
Before documenting, determine the project type — it changes what documentation is needed:
Library — no main package, meant to be imported by other projects:
ExampleXxx functions, playground demos, pkg.go.dev renderingApplication/CLI — has main package, cmd/ directory, produces a binary or Docker image:
Both apply : function comments, README, CONTRIBUTING, CHANGELOG.
Architecture docs : for complex projects, use docs/ directory and design description docs.
Every Go project needs these (ordered by priority):
| Item | Required | Library | Application |
|---|---|---|---|
| Doc comments on exported functions | Yes | Yes | Yes |
Package comment (// Package foo...) — MUST exist | 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 |
A private project might not need documentation website, llms.txt, Go Playground demos...
When documenting a large codebase with many packages, use up to 5 parallel sub-agents (via the Agent tool) for independent tasks:
ExampleXxx test functions for multiple packages simultaneouslyEvery 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:
// 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 Deprecated: markers and BUG: notes.
README SHOULD follow this exact section order. Copy the template from templates/README.md:
# headingCommon badges for Go projects:
[](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.
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.
For Go libraries, add these on top of the basics:
// Play: https://go.dev/play/p/xxx. Use the go-playground MCP tool when available to create and share playground URLs.func ExampleXxx() in _test.go files. These are executable documentation verified by go test.go doc locally to preview.See Library Documentation for details.
For Go applications/CLIs:
go install, Docker images, Homebrew...--help comprehensive; it's the primary documentationSee Application Documentation for details.
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.
Make your project consumable by AI agents:
llms.txt file at the repository root. Copy the template from templates/llms.txt. This file gives LLMs a structured overview of your project.Document how users get your project:
Libraries:
go get github.com/{owner}/{repo}
Applications:
# Pre-built binary
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
# Docker
docker pull {registry}/{owner}/{repo}:latest
See Project Docs for Dockerfile best practices and Homebrew tap setup.
Weekly Installs
101
Repository
GitHub Stars
184
First Seen
3 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode82
codex81
gemini-cli81
cursor81
kimi-cli80
github-copilot80
Claude技能创建器指南:构建模块化AI技能包,优化工作流与工具集成
5,700 周安装
| Yes |
| Yes |
| CHANGELOG.md or GitHub Releases | Recommended | Yes | Yes |
Example test functions (ExampleXxx) | 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 |