Skip to content
Agentmart
Submit a tool
This entry duplicates golang-patterns, which is the one we list in search.

golang-patterns

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

Skill by Luohaothu

IndexedMIT★ 26
Write a review

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/luohaothu-golang-patterns-cd144f

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

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

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions: not checked yet.

What's inside

Skill name
golang-patterns
Description
Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
License
MIT
Instructions
670 lines in SKILL.md · view on GitHub

Before installing

  • Instructions call the network

Agent Skills spec issues

  • body: 670 lines (spec recommends under 500)

Files (1)

  • SKILL.md

SKILL.md (first lines)

# 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) {
    // ...
}
```

## 錯誤處理模式

### 帶上下文的錯誤包裝

```go
// 良好:包裝錯誤並加上上下文
func LoadConfig(path string) (*Config, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, fmt.Errorf("load config %s: %w", path, err)
    }

    var cfg Config
    if err := json.Unmarshal(data, &cfg); err != nil {
        return nil, fmt.Errorf("parse config %s: %w", path, err)
    }

    return &cfg, nil
}
```

### 自訂錯誤類型

```go
// 定義領域特定錯誤
type ValidationError struct {
    Field   string
    Message string
}

func (e *ValidationError) Error() string {
    return fmt.Sprintf("validation failed on %s: %s", e.Field, e.Message)
}

From Luohaothu/everything-codex, MIT.

More from Luohaothu/everything-codex

Signals

Stars
26
Forks
5
Open issues
0
Last commit (this folder)
Jan 29, 2026 (9 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

Same skill elsewhere

Copies of this SKILL.md published by others; search shows only one of them.

More from Luohaothu

Cite this page

golang-patterns skill. Agent Mart. https://agentmart.dev/skills/luohaothu-golang-patterns-cd144f (last updated 2026-10-24)