Golang Patterns

Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.

2,037 stars 332 forksSoftware Development Plugin marketplace

Install

Claude Code plugin
/plugin marketplace add xu-xiang/everything-claude-code-zh

The repo is a Claude Code plugin marketplace. Run /plugin afterwards to pick the plugin that includes this skill.

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add xu-xiang/everything-claude-code-zh --skill golang-patterns
Manual install
git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git /tmp/everything-claude-code-zh
mkdir -p ~/.claude/skills
cp -r /tmp/everything-claude-code-zh/docs/zh-TW/skills/golang-patterns ~/.claude/skills/golang-patterns

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

# Go 開發模式

用於建構穩健、高效且可維護應用程式的慣用 Go 模式和最佳實務。

## 何時啟用

- 撰寫新的 Go 程式碼
- 審查 Go 程式碼
- 重構現有 Go 程式碼
- 設計 Go 套件/模組

## 核心原則

### 1. 簡單與清晰

Go 偏好簡單而非聰明。程式碼應該明顯且易讀。

```go
// 良好:清晰直接
func GetUser(id string) (*User, error) {
    user, err := db.FindUser(id)
    if err != nil {
        return nil, fmt.Errorf("get user %s: %w", id, err)
    }
    return user, nil
}

// 不良:過於聰明
func GetUser(id string) (*User, error) {
    return func() (*User, error) {
        if u, e := db.FindUser(id); e == nil {
            return u, nil
        } else {
            return nil, e
        }
    }()
}
```

### 2. 讓零值有用

設計類型使其零值無需初始化即可立即使用。

```go
// 良好:零值有用
type Counter struct {
    mu    sync.Mutex
    count int // 零值為 0,可直接使用
}

func (c *Counter) Inc() {
    c.mu.Lock()
    c.count++
    c.mu.Unlock()
}

// 良好:bytes.Buffer 零值可用
var buf bytes.Buffer
buf.WriteString("hello")

// 不良:需要初始化
type BadCounter struct {
    counts map[string]int // nil map 會 panic
}
```

### 3. 接受介面,回傳結構

函式應接受介面參數並回傳具體類型。

```go
// 良好:接受介面,回傳具體類型
func ProcessData(r io.Reader) (*Result, error) {
    data, err := io.ReadAll(r)
    if err != nil {
        return nil, err
    }
    return &Result{Data: data}, nil
}

// 不良:回傳介面(不必要地隱藏實作細節)
func ProcessData(r io.Reader) (io.Reader, error) {
    // ...
}
```

## 錯誤處理模式

### 帶上下文的錯誤包裝
Read the full skill on GitHub

Details

Skill name
golang-patterns
Path
docs/zh-TW/skills/golang-patterns/SKILL.md
License
MIT
Repo updated
5 March 2026

FAQ

What does the Golang Patterns skill do?

Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.

How do I install the Golang Patterns skill?

Claude Code plugin: /plugin marketplace add xu-xiang/everything-claude-code-zh. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Golang Patterns popular?

It lives in xu-xiang/everything-claude-code-zh, which has 2,037 GitHub stars and 332 forks.

Related skills

Browse the most popular Claude skills, all software development skills, or write your own with the skills generator.