跳到正文
Skillpedia
通用编程41k · #6798

go-concurrency-patterns

它能做什么

这个工具可以帮助你理解并使用 Go 程序中同时处理多个任务的进阶方法。它会教你如何使用“goroutine”(一起运行的小任务)和“channel”(这些任务之间的通信管道)。

适用场景

  • ✓构建需要同时执行多项任务的 Go 程序时。
  • ✓需要让任务高效协作(比如“工作池”模式)时。
  • ✓程序出现崩溃或并发错误、需要排查原因时。

技能链接

https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/go-concurrency-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/go-concurrency-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“如何让我的 Go 程序的两个部分同时运行,并安全地交换信息?”

每周 5 个新技能,1 分钟读完。

工作原理

它会讲解 Go 并发的基本构建模块,比如 goroutine 和 channel,并通过示例展示如何用它们进行通信和同步。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Go Concurrency Patterns

Production patterns for Go concurrency including goroutines, channels, synchronization primitives, and context management.

When to Use This Skill

  • Building concurrent Go applications
  • Implementing worker pools and pipelines
  • Managing goroutine lifecycles
  • Using channels for communication
  • Debugging race conditions
  • Implementing graceful shutdown

Core Concepts

1. Go Concurrency Primitives

Primitive Purpose
goroutine Lightweight concurrent execution
channel Communication between goroutines
select Multiplex channel operations
sync.Mutex Mutual exclusion
sync.WaitGroup Wait for goroutines to complete
context.Context Cancellation and deadlines

2. Go Concurrency Mantra

Don't communicate by sharing memory;
share memory by communicating.

Quick Start

package main

import (
    "context"
    "fmt"
    "sync"
    "time"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    results := make(chan string, 10)
    var wg sync.WaitGroup

    // Spawn workers
    for i := 0; i < 3; i++ {
        wg.Add(1)
        go worker(ctx, i, results, &wg)
    }

    // Close results when done
    go func() {
        wg.Wait()
        close(results)
    }()

    // Collect results
    for result := range results {
        fmt.Println(result)
    }
}

func worker(ctx context.Context, id int, results chan<- string, wg *sync.WaitGroup) {
    defer wg.Done()

    select {
    case <-ctx.Done():
        return
    case results <- fmt.Sprintf("Worker %d done", id):
    }
}

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's

  • Use context - For cancellation and deadlines
  • Close channels - From sender side only
  • Use errgroup - For concurrent operations with errors
  • Buffer channels - When you know the count
  • Prefer channels - Over mutexes when possible

Don'ts

  • Don't leak goroutines - Always have exit path
  • Don't close from receiver - Causes panic
  • Don't use shared memory - Unless necessary
  • Don't ignore context cancellation - Check ctx.Done()
  • Don't use time.Sleep for sync - Use proper primitives
常见问题

什么是 goroutine?

它可以理解为一种能与程序其他部分同时运行、互不干扰的函数。

channel 有什么用?

它就像管道,goroutine 可以通过它安全地互相发送和接收消息。

“不要通过共享内存来通信,而要通过通信来共享内存”是什么意思?

这是一条建议:避免让程序的多个部分同时访问同一份数据(这样容易出错),而是改用 channel 在它们之间传递数据。

详情

作者的原始描述

Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.

repo: wshobson/agents

热度

GitHub 星标
41k
排名
#6798
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能