All skills

golang-patterns

Official
by Api.AirforcePrepends a system promptAI & Agent Building000 uses202,700

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

open-sourceclaude-codeai-agent-buildingaffaan-m
Share

What this skill does

When applied, it prepends a system prompt before your request is sent — no extra calls and no change to how you are billed beyond the added tokens.

---
name: golang-patterns
description: Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
---

# 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

Use this skill

Per request

Add a "skill" field with the skill’s ID to your chat completion request. It is applied server-side before your prompt is sent — no extra calls.

{
  "model": "gpt-4o-mini",
  "skill": "imp-fef9e1ad-2fcc-4563-8afd-bac5111924e6",
  "messages": [{ "role": "user", "content": "…" }]
}
Always on — no field to send

Install the skill, enable it in your dashboard and (optionally) limit it to specific models. It then applies automatically to every matching request — with no "skill" field to send each time.

Set it up in your dashboard