本文へスキップ
Skillpedia
一般的なプログラミング48k · #5892

go

できること

このガイドでは、Go言語でコードを書くためのベストプラクティスを学べます。エラーの処理、スライス(リスト)とマップ(辞書)の扱い方、ゴルーチンとチャネルによる並行処理を、効率よくわかりやすく行う方法を紹介しています。

使う場面

  • ✓Go言語のプログラミングを学び始めたとき。
  • ✓既存のGoコードを見直したり改善したりするとき。
  • ✓よくあるGoのエラーに遭遇したとき。

スキルのリンク

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/go/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/go/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“Goでエラーを正しく扱うにはどうすればよいですか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

ガイドでは、「悪い」コード例と「良い」コード例を比較して、推奨される書き方を示します。なぜ一方の方法がより良いのかを、わかりやすさと効率の観点から説明します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Go: Idiomatic Efficiency Reference

When to Use

  • Use this skill when the task matches this description: Language-specific super-code guidelines for go.

Table of Contents

  1. Error Handling
  2. Slices & Maps
  3. Goroutines & Channels
  4. Structs & Interfaces
  5. Functions & Closures
  6. Anti-patterns specific to Go

1. Error Handling {#errors}

// ❌ Ignoring errors
result, _ := os.Open(path)

// ✅ — always handle; only use _ when error is provably irrelevant
result, err := os.Open(path)
if err != nil {
    return fmt.Errorf("open %s: %w", path, err)
}
// ❌ Redundant error variable
err := doA()
if err != nil { return err }
err = doB()
if err != nil { return err }

// ✅ — each :=/:  is fine; this is idiomatic Go. Don't try to "fix" it.
// What you CAN simplify: collapsing to one-liners where the if body is a single return
if err := doA(); err != nil { return err }
if err := doB(); err != nil { return err }
// ❌ Custom error type with no added value
type MyError struct{ msg string }
func (e MyError) Error() string { return e.msg }

// ✅ — use errors.New or fmt.Errorf unless callers need to inspect type
var ErrNotFound = errors.New("not found")
return fmt.Errorf("lookup %q: %w", key, ErrNotFound)

Wrap errors with %w (not %v) so callers can use errors.Is / errors.As.


2. Slices & Maps {#slices}

// ❌ Growing a slice without pre-allocation when size is known
var result []string
for _, item := range items {
    result = append(result, item.Name)
}

// ✅
result := make([]string, 0, len(items))
for _, item := range items {
    result = append(result, item.Name)
}
// ❌ Manual existence check before map write
if _, ok := m[key]; !ok {
    m[key] = []string{}
}
m[key] = append(m[key], value)

// ✅ — append to nil slice is valid Go
m[key] = append(m[key], value)
// ❌ Copying a map by assignment (copies reference)
copy := original

// ✅
copy := make(map[K]V, len(original))
for k, v := range original { copy[k] = v }

3. Goroutines & Channels {#concurrency}

// ❌ Fire-and-forget goroutine with no lifecycle
go doWork()

// ✅ — use errgroup or WaitGroup to track completion
var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    doWork()
}()
wg.Wait()
// ❌ Unbuffered channel causing unnecessary goroutine block
ch := make(chan Result)
go func() { ch <- compute() }()
result := <-ch

// ✅ — for single-result, buffered channel avoids goroutine leak if receiver exits early
ch := make(chan Result, 1)
go func() { ch <- compute() }()
result := <-ch
// ❌ select with a busy-wait default
for {
    select {
    case v := <-ch:
        process(v)
    default:
        // spin
    }
}

// ✅ — blocking select unless you genuinely need non-blocking
for v := range ch {
    process(v)
}

Use golang.org/x/sync/errgroup for fan-out with error collection.


4. Structs & Interfaces {#structs}

// ❌ Large interface
type Storage interface {
    Get(key string) ([]byte, error)
    Set(key string, val []byte) error
    Delete(key string) error
    List(prefix string) ([]string, error)
    // ... 10 more methods
}

// ✅ — small, composable interfaces
type Getter interface { Get(key string) ([]byte, error) }
type Setter interface { Set(key string, val []byte) error }
type Storage interface { Getter; Setter }
// ❌ Returning concrete struct from constructor (ties callers to implementation)
func NewStore() *RedisStore { ... }

// ✅ — return interface when you have or anticipate multiple implementations
func NewStore() Storage { return &RedisStore{...} }
// ❌ Pointer receiver for tiny value types
func (p *Point) X() float64 { return p.x }

// ✅ — value receiver for small immutable types
func (p Point) X() float64 { return p.x }

Rule: pointer receiver when method mutates state OR struct is large (>3 fields of non-trivial size). Value receiver otherwise.


5. Functions & Closures {#functions}

// ❌ Named return values used just to avoid a variable declaration
func divide(a, b float64) (result float64, err error) {
    result = a / b
    return
}

// ✅ — named returns are worth it only for deferred mutation or documentation
func divide(a, b float64) (float64, error) {
    if b == 0 { return 0, errors.New("division by zero") }
    return a / b, nil
}

…
よくある質問

「ゴルーチン」と「チャネル」とは何ですか?

ゴルーチンは複数の処理を同時に実行するためのGoの仕組みで、チャネルはそれらの処理が安全に情報をやり取りするための仕組みです。

`%w`でエラーを「ラップ」するのはなぜ重要ですか?

`%w`を使うと、エラーを受け取った側が、そのエラーが特定のエラーに由来するものかどうかを、`errors.Is`や`errors.As`などの関数で確認できるようになります。

「アンチパターン」とは何ですか?

動くには動くものの、後になって混乱しやすく、非効率で、エラーを起こしやすいコードにつながるプログラミングの書き方です。このガイドでは、Goでそれを避ける方法を示しています。

詳細

作者による元の説明

Language-specific super-code guidelines for go.

repo: sickn33/agentic-awesome-skills

人気

GitHubのスター
48k
ランキング
#5892
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル