---
name: Golang Expert
slug: golang-expert
category: Quality
description: Golang Expert provides guidance on Go style, concurrency, error handling, testing, and project structure. Use it when writing or reviewing Go code and organizing Go applications.
github: "https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/golang"
language: Python
stars: 688
forks: 92
install: "npx degit https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/golang ~/.claude/skills/golang"
installs_to: ~/.claude/skills/golang
source_path: kaiwu/builtin_experts/golang/SKILL.md
collection_size: 19
category_size: 1354
collection_url: "https://dirskills.com/collections/val1813/kwcode"
added: 2026-08-12T04:43:48.931Z
last_synced: 2026-08-12T04:43:48.931Z
canonical_url: "https://dirskills.com/skills/golang-expert"
---

# Golang Expert

Golang Expert provides guidance on Go style, concurrency, error handling, testing, and project structure. Use it when writing or reviewing Go code and organizing Go applications.

**Install:**

```bash
npx degit https://github.com/val1813/kwcode/tree/master/kaiwu/builtin_experts/golang ~/.claude/skills/golang
```

## README

## 领域知识

你是Go语言专家。

### 代码风格
- 遵循 Go 官方风格指南（Effective Go）
- 错误处理：`if err != nil { return err }` 模式，不要忽略错误
- 命名：驼峰命名，导出用大写开头，包名小写单词
- 接口命名：单方法接口用 `-er` 后缀（Reader, Writer, Closer）
- 避免 `init()` 函数，除非绝对必要

### 并发模式
- 优先使用 channel 通信，而非共享内存
- goroutine 必须有明确的退出机制（context.Cancel, done channel）
- 使用 `sync.WaitGroup` 等待多个 goroutine
- 避免 goroutine 泄漏：确保所有启动的 goroutine 都能正常退出
- `select` 语句必须包含 `default` 或 `context.Done()` 分支

### 错误处理
- 使用 `fmt.Errorf("context: %w", err)` 包装错误
- 自定义错误类型实现 `Error()` 接口
- 使用 `errors.Is()` 和 `errors.As()` 检查错误
- 不要 panic，除非是不可恢复的程序错误

### 测试
- 测试文件命名：`xxx_test.go`，与源文件同包
- 表驱动测试（table-driven tests）是标准模式
- 使用 `t.Helper()` 标记辅助函数
- 基准测试用 `Benchmark` 前缀
- 运行测试：`go test ./...`

### 项目结构
- `cmd/` 放可执行入口
- `internal/` 放不导出的包
- `pkg/` 放可复用的库代码
- 不要过度分包，Go 鼓励扁平结构

## 经验规则（自动生成）
