Go Stdlib Style 标志

Go Stdlib Style

社区
happydog-intj
go-stdlib-style

Go 标准库编码风格 Skill。蒸馏自 golang/go 标准库源码、Effective Go、 Rob Pike 的 Go Proverbs、Russ Cox 的设计文档、Go Code Review Comments 官方指南。 触发词:「Go 标准库风格」「idiomatic Go」「地道 Go」「go stdlib style」「Rob Pike 风格」。 适用:Go 库开发、CLI 工具、网络服务、系统工具。

概览

发布者happydog-intj
代码仓库luban-skill
技能名称go-stdlib-style
星标数
32
复刻数
3
附带文件
仅含指令
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 happydog-intj 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Go Stdlib Style AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/happydog-intj/luban-skill/tree/master/skills/go-stdlib.skill
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/happydog-intj/luban-skill.git /tmp/luban-skill
mkdir -p .claude/skills
cp -r /tmp/luban-skill/skills/go-stdlib.skill .claude/skills/go-stdlib-style
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Go Stdlib Style,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Go Stdlib Style。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Go Stdlib Style 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Go 标准库 · 编码 DNA

"Clear is better than clever." — Rob Pike, Go Proverbs "A little copying is better than a little dependency." — Rob Pike

角色定义

此 Skill 激活后,你写出的 Go 代码应该让标准库 contributor 在 code review 时感觉 「这代码像是从 net/http 里拿出来的」,而不是「这是 Java 开发者写的 Go」。

这意味着:简单、显式、接口小、无魔法。


命名 DNA

6 条直觉规则:

  1. 包名:单词、小写、不下划线、不 camelCase

    go
    // ✅
    package http
    package json
    package sync
    
    // ❌
    package httpUtils
    package http_client
    package HTTPServer

    包名是命名空间,使用时 http.Get,不是 httpClient.Get。

  2. 导出名不重复包名

    go
    // ✅ 使用时是 http.Request,不是 http.HTTPRequest
    package http
    type Request struct { ... }
    func Get(url string) (*Response, error) { ... }
    
    // ❌ 冗余
    type HTTPRequest struct { ... }
    func HTTPGet(url string) (*HTTPResponse, error) { ... }
  3. 接口名 = 方法名 + er(单方法接口)

    go
    // ✅ Go 的核心约定
    type Reader interface { Read(p []byte) (n int, err error) }
    type Writer interface { Write(p []byte) (n int, err error) }
    type Stringer interface { String() string }
    type Handler interface { ServeHTTP(ResponseWriter, *Request) }
    
    // ❌ 动词/形容词命名接口
    type Readable interface { ... }
    type DataProcessor interface { ... }
  4. 变量名短而精,作用域小则短

    go
    // ✅ 局部变量
    for i, v := range items { ... }
    r, err := http.Get(url)
    buf := make([]byte, 1024)
    
    // ✅ 包级变量
    var defaultClient = &Client{}
    var ErrNotFound = errors.New("not found")
    
    // ❌ 过长(Go 不是 Java)
    for index, value := range itemList { ... }
    httpResponseReader, requestError := httpClient.MakeGetRequest(requestUrl)
  5. 错误变量叫 err,不叫 error 或 e

    go
    // ✅
    n, err := r.Read(buf)
    if err != nil {
        return err
    }
  6. 常量和包级变量不用全大写(除了真正的常量)

    go
    // ✅
    const maxRetries = 3
    var defaultTimeout = 30 * time.Second
    
    // ❌ (Go 不是 C)
    const MAX_RETRIES = 3
    var DEFAULT_TIMEOUT = 30 * time.Second

结构偏好

接口设计:接口越小越好

go
// ✅ 单方法接口,组合使用
type Reader interface { Read(p []byte) (n int, err error) }
type Writer interface { Write(p []byte) (n int, err error) }
type ReadWriter interface {
    Reader
    Writer
}

// ❌ 胖接口(难以实现,难以测试)
type Storage interface {
    Read() ([]byte, error)
    Write([]byte) error
    Delete(key string) error
    List() ([]string, error)
    Stats() Stats
    Flush() error
}

函数设计:

go
// ✅ 接受接口,返回具体类型
func NewScanner(r io.Reader) *Scanner { ... }  // 接受 Reader 接口

// ✅ 错误永远是最后一个返回值
func Open(name string) (*File, error) { ... }

// ✅ 选项用 struct,不用过多参数
type DialOption struct {
    Timeout   time.Duration
    KeepAlive time.Duration
    Resolver  Resolver
}
func DialContext(ctx context.Context, network, address string, opt ...DialOption) (Conn, error)
// 或更 Go 风格的 functional options

错误处理:显式、不忽略、包上下文

go
// ✅ 每个错误都处理
f, err := os.Open(filename)
if err != nil {
    return fmt.Errorf("open config file: %w", err)  // 用 %w 包装,保留原始错误
}
defer f.Close()

// ✅ 错误信息小写,不加标点(它是被包装的,不是最终输出)
return fmt.Errorf("parse response: %w", err)   // ✅
return fmt.Errorf("Parse Response: %w", err)   // ❌ 大写
return fmt.Errorf("parse response: %w.", err)  // ❌ 加句号

// ❌ 忽略错误(除非真的不重要且有注释说明)
f, _ := os.Open(filename)

并发:

go
// ✅ 用 channel 传数据,不共享内存
func producer(ch chan<- int) {
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch)
}

// ✅ sync.Mutex 保护共享状态,锁和被保护的数据放一起
type Cache struct {
    mu    sync.Mutex  // 锁紧跟着被保护的字段
    items map[string]Item
}

// ✅ context 作为第一个参数传递
func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) { ... }

注释哲学

Go 的注释规则极其简单,但要求严格:

go
// ✅ 导出符号必须有注释,且以符号名开头
// Reader is the interface that wraps the basic Read method.
type Reader interface { ... }

// Open opens the named file for reading.
// If successful, methods on the returned file can be used for reading;
// the associated file descriptor has mode O_RDONLY.
// If there is an error, it will be of type *PathError.
func Open(name string) (*File, error) { ... }

// ErrNotExist indicates that a file or directory does not exist.
var ErrNotExist = errors.New("file does not exist")
go
// ❌ 注释不以符号名开头(go doc 解析失败)
// This opens a file.
func Open(name string) (*File, error) { ... }

// ❌ 废话注释
// i is an integer counter
i := 0

//go: 指令注释:

go
//go:generate stringer -type=Weekday
//go:noescape  // 告诉编译器参数不逃逸到堆
//nolint:errcheck  // 关闭 linter(要有充分理由)

反模式(绝不这样写)

  1. panic 用于业务逻辑

    go
    // ❌ panic 不是错误处理
    func GetUser(id int) *User {
        user := db.Find(id)
        if user == nil {
            panic("user not found")
        }
        return user
    }
    // ✅ 返回 error
    func GetUser(id int) (*User, error) { ... }
  2. 接受具体类型而不是接口(限制了可测试性)

    go
    // ❌ 接受具体类型,无法 mock
    func Process(c *http.Client) { ... }
    // ✅ 接受接口
    func Process(c interface { Do(*http.Request) (*http.Response, error) }) { ... }
  3. init() 函数做复杂初始化 — init 难以测试、难以控制执行顺序

  4. 全局变量暴露可变状态 — 包级全局变量应该是不可变的(常量或配置)

  5. interface{} / any 满天飞 — Go 1.18+ 用泛型,不用空接口甩锅

    go
    // ❌
    func Store(key string, value interface{}) { ... }
    // ✅ 泛型
    func Store[V any](key string, value V) { ... }
  6. 忽略 defer 的错误

    go
    defer f.Close()  // ❌ 文件写入后 Close 出错会丢数据
    defer func() {
        if err := f.Close(); err != nil {
            log.Printf("close file: %v", err)
        }
    }()  // ✅
  7. 错误信息大写或加句号 — 会破坏 fmt.Errorf("open: %w", err) 的拼接效果


校验测试

  1. gofmt 检查:gofmt -d . 有输出吗?有就是格式不对(gofmt 是宗教)
  2. 接口检查:接收参数能换成接口吗?换了之后可测试性提升吗?
  3. 错误路径检查:grep -n "_, err" *.go —— 有没有忽略的错误?

来源

常见问题

Go Stdlib Style AI 技能有什么作用?

Go 标准库编码风格 Skill。蒸馏自 golang/go 标准库源码、Effective Go、 Rob Pike 的 Go Proverbs、Russ Cox 的设计文档、Go Code Review Comments 官方指南。 触发词:「Go 标准库风格」「idiomatic Go」「地道 Go」「go stdlib style」「Rob Pike 风格」。 适用:Go 库开发、CLI 工具、网络服务、系统工具。

为什么在 TypingMind 中使用 Go Stdlib Style?

因为您只需安装一次,就能搭配任何模型使用。Go Stdlib Style 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Go Stdlib Style?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/happydog-intj/luban-skill/tree/master/skills/go-stdlib.skill。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Go Stdlib Style?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Go Stdlib Style 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Go Stdlib Style 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Go Stdlib Style AI 技能免费吗?

happydog-intj 已在 GitHub 上发布了该技能。请查看代码仓库了解许可条款。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇