[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M70NYSPPBMMPMJW6YEJ2NADB":3,"skill-same:01M70NYSPPBMMPMJW6YEJ2NADB":67},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":38,"linkOnly":39,"markdownContent":40,"markdownBody":41,"frontMatter":42,"refName":43,"commitSha":44,"skillDirectory":45,"forksCount":46,"repositoryHtmlUrl":47,"repositoryHomepageUrl":48,"ownerHtmlUrl":49,"ownerBlogUrl":50,"ownerDescription":51,"updatedAt":52,"install":53,"resources":66},"01M70NYSPPBMMPMJW6YEJ2NADB","go-concurrency-patterns",null,"Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.","shengdabai\u002FTony-Claude-Code-Skills","shengdabai","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F116417419?v=4","01M70F6D2WY5PQV5A92BKYJQEW","Tony Sheng","01M70F6D3GAAAYZ5H4CC1G8VXM",3,"MIT","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fwshobson-agents\u002Fplugins\u002Fsystems-programming\u002Fskills\u002Fgo-concurrency-patterns\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],"go","patterns","go concurrency","concurrency patterns","go concurrency patterns","concurrency","with","channels","goroutines","use","applications","context","primitives","master","sync","when","debugging","worker","implementing","2026-11-10 10:12:30",false,"---\nname: go-concurrency-patterns\ndescription: Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.\n---\n\n# Go Concurrency Patterns\n\nProduction patterns for Go concurrency including goroutines, channels, synchronization primitives, and context management.\n\n## When to Use This Skill\n\n- Building concurrent Go applications\n- Implementing worker pools and pipelines\n- Managing goroutine lifecycles\n- Using channels for communication\n- Debugging race conditions\n- Implementing graceful shutdown\n\n## Core Concepts\n\n### 1. Go Concurrency Primitives\n\n| Primitive | Purpose |\n|-----------|---------|\n| `goroutine` | Lightweight concurrent execution |\n| `channel` | Communication between goroutines |\n| `select` | Multiplex channel operations |\n| `sync.Mutex` | Mutual exclusion |\n| `sync.WaitGroup` | Wait for goroutines to complete |\n| `context.Context` | Cancellation and deadlines |\n\n### 2. Go Concurrency Mantra\n\n```\nDon't communicate by sharing memory;\nshare memory by communicating.\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nfunc main() {\n    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\n    defer cancel()\n\n    results := make(chan string, 10)\n    var wg sync.WaitGroup\n\n    \u002F\u002F Spawn workers\n    for i := 0; i \u003C 3; i++ {\n        wg.Add(1)\n        go worker(ctx, i, results, &wg)\n    }\n\n    \u002F\u002F Close results when done\n    go func() {\n        wg.Wait()\n        close(results)\n    }()\n\n    \u002F\u002F Collect results\n    for result := range results {\n        fmt.Println(result)\n    }\n}\n\nfunc worker(ctx context.Context, id int, results chan\u003C- string, wg *sync.WaitGroup) {\n    defer wg.Done()\n\n    select {\n    case \u003C-ctx.Done():\n        return\n    case results \u003C- fmt.Sprintf(\"Worker %d done\", id):\n    }\n}\n```\n\n## Patterns\n\n### Pattern 1: Worker Pool\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n)\n\ntype Job struct {\n    ID   int\n    Data string\n}\n\ntype Result struct {\n    JobID int\n    Output string\n    Err   error\n}\n\nfunc WorkerPool(ctx context.Context, numWorkers int, jobs \u003C-chan Job) \u003C-chan Result {\n    results := make(chan Result, len(jobs))\n\n    var wg sync.WaitGroup\n    for i := 0; i \u003C numWorkers; i++ {\n        wg.Add(1)\n        go func(workerID int) {\n            defer wg.Done()\n            for job := range jobs {\n                select {\n                case \u003C-ctx.Done():\n                    return\n                default:\n                    result := processJob(job)\n                    results \u003C- result\n                }\n            }\n        }(i)\n    }\n\n    go func() {\n        wg.Wait()\n        close(results)\n    }()\n\n    return results\n}\n\nfunc processJob(job Job) Result {\n    \u002F\u002F Simulate work\n    return Result{\n        JobID:  job.ID,\n        Output: fmt.Sprintf(\"Processed: %s\", job.Data),\n    }\n}\n\n\u002F\u002F Usage\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n\n    jobs := make(chan Job, 100)\n\n    \u002F\u002F Send jobs\n    go func() {\n        for i := 0; i \u003C 50; i++ {\n            jobs \u003C- Job{ID: i, Data: fmt.Sprintf(\"job-%d\", i)}\n        }\n        close(jobs)\n    }()\n\n    \u002F\u002F Process with 5 workers\n    results := WorkerPool(ctx, 5, jobs)\n\n    for result := range results {\n        fmt.Printf(\"Result: %+v\\n\", result)\n    }\n}\n```\n\n### Pattern 2: Fan-Out\u002FFan-In Pipeline\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"sync\"\n)\n\n\u002F\u002F Stage 1: Generate numbers\nfunc generate(ctx context.Context, nums ...int) \u003C-chan int {\n    out := make(chan int)\n    go func() {\n        defer close(out)\n        for _, n := range nums {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n:\n            }\n        }\n    }()\n    return out\n}\n\n\u002F\u002F Stage 2: Square numbers (can run multiple instances)\nfunc square(ctx context.Context, in \u003C-chan int) \u003C-chan int {\n    out := make(chan int)\n    go func() {\n        defer close(out)\n        for n := range in {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n * n:\n            }\n        }\n    }()\n    return out\n}\n\n\u002F\u002F Fan-in: Merge multiple channels into one\nfunc merge(ctx context.Context, cs ...\u003C-chan int) \u003C-chan int {\n    var wg sync.WaitGroup\n    out := make(chan int)\n\n    \u002F\u002F Start output goroutine for each input channel\n    output := func(c \u003C-chan int) {\n        defer wg.Done()\n        for n := range c {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n:\n            }\n        }\n    }\n\n    wg.Add(len(cs))\n    for _, c := range cs {\n        go output(c)\n    }\n\n    \u002F\u002F Close out after all inputs are done\n    go func() {\n        wg.Wait()\n        close(out)\n    }()\n\n    return out\n}\n\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n\n    \u002F\u002F Generate input\n    in := generate(ctx, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n\n    \u002F\u002F Fan out to multiple squarers\n    c1 := square(ctx, in)\n    c2 := square(ctx, in)\n    c3 := square(ctx, in)\n\n    \u002F\u002F Fan in results\n    for result := range merge(ctx, c1, c2, c3) {\n        fmt.Println(result)\n    }\n}\n```\n\n### Pattern 3: Bounded Concurrency with Semaphore\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"golang.org\u002Fx\u002Fsync\u002Fsemaphore\"\n    \"sync\"\n)\n\ntype RateLimitedWorker struct {\n    sem *semaphore.Weighted\n}\n\nfunc NewRateLimitedWorker(maxConcurrent int64) *RateLimitedWorker {\n    return &RateLimitedWorker{\n        sem: semaphore.NewWeighted(maxConcurrent),\n    }\n}\n\nfunc (w *RateLimitedWorker) Do(ctx context.Context, tasks []func() error) []error {\n    var (\n        wg     sync.WaitGroup\n        mu     sync.Mutex\n        errors []error\n    )\n\n    for _, task := range tasks {\n        \u002F\u002F Acquire semaphore (blocks if at limit)\n        if err := w.sem.Acquire(ctx, 1); err != nil {\n            return []error{err}\n        }\n\n        wg.Add(1)\n        go func(t func() error) {\n            defer wg.Done()\n            defer w.sem.Release(1)\n\n            if err := t(); err != nil {\n                mu.Lock()\n                errors = append(errors, err)\n                mu.Unlock()\n            }\n        }(task)\n    }\n\n    wg.Wait()\n    return errors\n}\n\n\u002F\u002F Alternative: Channel-based semaphore\ntype Semaphore chan struct{}\n\nfunc NewSemaphore(n int) Semaphore {\n    return make(chan struct{}, n)\n}\n\nfunc (s Semaphore) Acquire() {\n    s \u003C- struct{}{}\n}\n\nfunc (s Semaphore) Release() {\n    \u003C-s\n}\n```\n\n### Pattern 4: Graceful Shutdown\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"os\"\n    \"os\u002Fsignal\"\n    \"sync\"\n    \"syscall\"\n    \"time\"\n)\n\ntype Server struct {\n    shutdown chan struct{}\n    wg       sync.WaitGroup\n}\n\nfunc NewServer() *Server {\n    return &Server{\n        shutdown: make(chan struct{}),\n    }\n}\n\nfunc (s *Server) Start(ctx context.Context) {\n    \u002F\u002F Start workers\n    for i := 0; i \u003C 5; i++ {\n        s.wg.Add(1)\n        go s.worker(ctx, i)\n    }\n}\n\nfunc (s *Server) worker(ctx context.Context, id int) {\n    defer s.wg.Done()\n    defer fmt.Printf(\"Worker %d stopped\\n\", id)\n\n    ticker := time.NewTicker(time.Second)\n    defer ticker.Stop()\n\n    for {\n        select {\n        case \u003C-ctx.Done():\n            \u002F\u002F Cleanup\n            fmt.Printf(\"Worker %d cleaning up...\\n\", id)\n            time.Sleep(500 * time.Millisecond) \u002F\u002F Simulated cleanup\n            return\n        case \u003C-ticker.C:\n            fmt.Printf(\"Worker %d working...\\n\", id)\n        }\n    }\n}\n\nfunc (s *Server) Shutdown(timeout time.Duration) {\n    \u002F\u002F Signal shutdown\n    close(s.shutdown)\n\n    \u002F\u002F Wait with timeout\n    done := make(chan struct{})\n    go func() {\n        s.wg.Wait()\n        close(done)\n    }()\n\n    select {\n    case \u003C-done:\n        fmt.Println(\"Clean shutdown completed\")\n    case \u003C-time.After(timeout):\n        fmt.Println(\"Shutdown timed out, forcing exit\")\n    }\n}\n\nfunc main() {\n    \u002F\u002F Setup signal handling\n    ctx, cancel := context.WithCancel(context.Background())\n\n    sigCh := make(chan os.Signal, 1)\n    signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)\n\n    server := NewServer()\n    server.Start(ctx)\n\n    \u002F\u002F Wait for signal\n    sig := \u003C-sigCh\n    fmt.Printf(\"\\nReceived signal: %v\\n\", sig)\n\n    \u002F\u002F Cancel context to stop workers\n    cancel()\n\n    \u002F\u002F Wait for graceful shutdown\n    server.Shutdown(5 * time.Second)\n}\n```\n\n### Pattern 5: Error Group with Cancellation\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"golang.org\u002Fx\u002Fsync\u002Ferrgroup\"\n    \"net\u002Fhttp\"\n)\n\nfunc fetchAllURLs(ctx context.Context, urls []string) ([]string, error) {\n    g, ctx := errgroup.WithContext(ctx)\n\n    results := make([]string, len(urls))\n\n    for i, url := range urls {\n        i, url := i, url \u002F\u002F Capture loop variables\n\n        g.Go(func() error {\n            req, err := http.NewRequestWithContext(ctx, \"GET\", url, nil)\n            if err != nil {\n                return fmt.Errorf(\"creating request for %s: %w\", url, err)\n            }\n\n            resp, err := http.DefaultClient.Do(req)\n            if err != nil {\n                return fmt.Errorf(\"fetching %s: %w\", url, err)\n            }\n            defer resp.Body.Close()\n\n            results[i] = fmt.Sprintf(\"%s: %d\", url, resp.StatusCode)\n            return nil\n        })\n    }\n\n    \u002F\u002F Wait for all goroutines to complete or one to fail\n    if err := g.Wait(); err != nil {\n        return nil, err \u002F\u002F First error cancels all others\n    }\n\n    return results, nil\n}\n\n\u002F\u002F With concurrency limit\nfunc fetchWithLimit(ctx context.Context, urls []string, limit int) ([]string, error) {\n    g, ctx := errgroup.WithContext(ctx)\n    g.SetLimit(limit) \u002F\u002F Max concurrent goroutines\n\n    results := make([]string, len(urls))\n    var mu sync.Mutex\n\n    for i, url := range urls {\n        i, url := i, url\n\n        g.Go(func() error {\n            result, err := fetchURL(ctx, url)\n            if err != nil {\n                return err\n            }\n\n            mu.Lock()\n            results[i] = result\n            mu.Unlock()\n            return nil\n        })\n    }\n\n    if err := g.Wait(); err != nil {\n        return nil, err\n    }\n\n    return results, nil\n}\n```\n\n### Pattern 6: Concurrent Map with sync.Map\n\n```go\npackage main\n\nimport (\n    \"sync\"\n)\n\n\u002F\u002F For frequent reads, infrequent writes\ntype Cache struct {\n    m sync.Map\n}\n\nfunc (c *Cache) Get(key string) (interface{}, bool) {\n    return c.m.Load(key)\n}\n\nfunc (c *Cache) Set(key string, value interface{}) {\n    c.m.Store(key, value)\n}\n\nfunc (c *Cache) GetOrSet(key string, value interface{}) (interface{}, bool) {\n    return c.m.LoadOrStore(key, value)\n}\n\nfunc (c *Cache) Delete(key string) {\n    c.m.Delete(key)\n}\n\n\u002F\u002F For write-heavy workloads, use sharded map\ntype ShardedMap struct {\n    shards    []*shard\n    numShards int\n}\n\ntype shard struct {\n    sync.RWMutex\n    data map[string]interface{}\n}\n\nfunc NewShardedMap(numShards int) *ShardedMap {\n    m := &ShardedMap{\n        shards:    make([]*shard, numShards),\n        numShards: numShards,\n    }\n    for i := range m.shards {\n        m.shards[i] = &shard{data: make(map[string]interface{})}\n    }\n    return m\n}\n\nfunc (m *ShardedMap) getShard(key string) *shard {\n    \u002F\u002F Simple hash\n    h := 0\n    for _, c := range key {\n        h = 31*h + int(c)\n    }\n    return m.shards[h%m.numShards]\n}\n\nfunc (m *ShardedMap) Get(key string) (interface{}, bool) {\n    shard := m.getShard(key)\n    shard.RLock()\n    defer shard.RUnlock()\n    v, ok := shard.data[key]\n    return v, ok\n}\n\nfunc (m *ShardedMap) Set(key string, value interface{}) {\n    shard := m.getShard(key)\n    shard.Lock()\n    defer shard.Unlock()\n    shard.data[key] = value\n}\n```\n\n### Pattern 7: Select with Timeout and Default\n\n```go\nfunc selectPatterns() {\n    ch := make(chan int)\n\n    \u002F\u002F Timeout pattern\n    select {\n    case v := \u003C-ch:\n        fmt.Println(\"Received:\", v)\n    case \u003C-time.After(time.Second):\n        fmt.Println(\"Timeout!\")\n    }\n\n    \u002F\u002F Non-blocking send\u002Freceive\n    select {\n    case ch \u003C- 42:\n        fmt.Println(\"Sent\")\n    default:\n        fmt.Println(\"Channel full, skipping\")\n    }\n\n    \u002F\u002F Priority select (check high priority first)\n    highPriority := make(chan int)\n    lowPriority := make(chan int)\n\n    for {\n        select {\n        case msg := \u003C-highPriority:\n            fmt.Println(\"High priority:\", msg)\n        default:\n            select {\n            case msg := \u003C-highPriority:\n                fmt.Println(\"High priority:\", msg)\n            case msg := \u003C-lowPriority:\n                fmt.Println(\"Low priority:\", msg)\n            }\n        }\n    }\n}\n```\n\n## Race Detection\n\n```bash\n# Run tests with race detector\ngo test -race .\u002F...\n\n# Build with race detector\ngo build -race .\n\n# Run with race detector\ngo run -race main.go\n```\n\n## Best Practices\n\n### Do's\n- **Use context** - For cancellation and deadlines\n- **Close channels** - From sender side only\n- **Use errgroup** - For concurrent operations with errors\n- **Buffer channels** - When you know the count\n- **Prefer channels** - Over mutexes when possible\n\n### Don'ts\n- **Don't leak goroutines** - Always have exit path\n- **Don't close from receiver** - Causes panic\n- **Don't use shared memory** - Unless necessary\n- **Don't ignore context cancellation** - Check ctx.Done()\n- **Don't use time.Sleep for sync** - Use proper primitives\n\n## Resources\n\n- [Go Concurrency Patterns](https:\u002F\u002Fgo.dev\u002Fblog\u002Fpipelines)\n- [Effective Go - Concurrency](https:\u002F\u002Fgo.dev\u002Fdoc\u002Feffective_go#concurrency)\n- [Go by Example - Goroutines](https:\u002F\u002Fgobyexample.com\u002Fgoroutines)\n","# Go Concurrency Patterns\n\nProduction patterns for Go concurrency including goroutines, channels, synchronization primitives, and context management.\n\n## When to Use This Skill\n\n- Building concurrent Go applications\n- Implementing worker pools and pipelines\n- Managing goroutine lifecycles\n- Using channels for communication\n- Debugging race conditions\n- Implementing graceful shutdown\n\n## Core Concepts\n\n### 1. Go Concurrency Primitives\n\n| Primitive | Purpose |\n|-----------|---------|\n| `goroutine` | Lightweight concurrent execution |\n| `channel` | Communication between goroutines |\n| `select` | Multiplex channel operations |\n| `sync.Mutex` | Mutual exclusion |\n| `sync.WaitGroup` | Wait for goroutines to complete |\n| `context.Context` | Cancellation and deadlines |\n\n### 2. Go Concurrency Mantra\n\n```\nDon't communicate by sharing memory;\nshare memory by communicating.\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nfunc main() {\n    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\n    defer cancel()\n\n    results := make(chan string, 10)\n    var wg sync.WaitGroup\n\n    \u002F\u002F Spawn workers\n    for i := 0; i \u003C 3; i++ {\n        wg.Add(1)\n        go worker(ctx, i, results, &wg)\n    }\n\n    \u002F\u002F Close results when done\n    go func() {\n        wg.Wait()\n        close(results)\n    }()\n\n    \u002F\u002F Collect results\n    for result := range results {\n        fmt.Println(result)\n    }\n}\n\nfunc worker(ctx context.Context, id int, results chan\u003C- string, wg *sync.WaitGroup) {\n    defer wg.Done()\n\n    select {\n    case \u003C-ctx.Done():\n        return\n    case results \u003C- fmt.Sprintf(\"Worker %d done\", id):\n    }\n}\n```\n\n## Patterns\n\n### Pattern 1: Worker Pool\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n)\n\ntype Job struct {\n    ID   int\n    Data string\n}\n\ntype Result struct {\n    JobID int\n    Output string\n    Err   error\n}\n\nfunc WorkerPool(ctx context.Context, numWorkers int, jobs \u003C-chan Job) \u003C-chan Result {\n    results := make(chan Result, len(jobs))\n\n    var wg sync.WaitGroup\n    for i := 0; i \u003C numWorkers; i++ {\n        wg.Add(1)\n        go func(workerID int) {\n            defer wg.Done()\n            for job := range jobs {\n                select {\n                case \u003C-ctx.Done():\n                    return\n                default:\n                    result := processJob(job)\n                    results \u003C- result\n                }\n            }\n        }(i)\n    }\n\n    go func() {\n        wg.Wait()\n        close(results)\n    }()\n\n    return results\n}\n\nfunc processJob(job Job) Result {\n    \u002F\u002F Simulate work\n    return Result{\n        JobID:  job.ID,\n        Output: fmt.Sprintf(\"Processed: %s\", job.Data),\n    }\n}\n\n\u002F\u002F Usage\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n\n    jobs := make(chan Job, 100)\n\n    \u002F\u002F Send jobs\n    go func() {\n        for i := 0; i \u003C 50; i++ {\n            jobs \u003C- Job{ID: i, Data: fmt.Sprintf(\"job-%d\", i)}\n        }\n        close(jobs)\n    }()\n\n    \u002F\u002F Process with 5 workers\n    results := WorkerPool(ctx, 5, jobs)\n\n    for result := range results {\n        fmt.Printf(\"Result: %+v\\n\", result)\n    }\n}\n```\n\n### Pattern 2: Fan-Out\u002FFan-In Pipeline\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"sync\"\n)\n\n\u002F\u002F Stage 1: Generate numbers\nfunc generate(ctx context.Context, nums ...int) \u003C-chan int {\n    out := make(chan int)\n    go func() {\n        defer close(out)\n        for _, n := range nums {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n:\n            }\n        }\n    }()\n    return out\n}\n\n\u002F\u002F Stage 2: Square numbers (can run multiple instances)\nfunc square(ctx context.Context, in \u003C-chan int) \u003C-chan int {\n    out := make(chan int)\n    go func() {\n        defer close(out)\n        for n := range in {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n * n:\n            }\n        }\n    }()\n    return out\n}\n\n\u002F\u002F Fan-in: Merge multiple channels into one\nfunc merge(ctx context.Context, cs ...\u003C-chan int) \u003C-chan int {\n    var wg sync.WaitGroup\n    out := make(chan int)\n\n    \u002F\u002F Start output goroutine for each input channel\n    output := func(c \u003C-chan int) {\n        defer wg.Done()\n        for n := range c {\n            select {\n            case \u003C-ctx.Done():\n                return\n            case out \u003C- n:\n            }\n        }\n    }\n\n    wg.Add(len(cs))\n    for _, c := range cs {\n        go output(c)\n    }\n\n    \u002F\u002F Close out after all inputs are done\n    go func() {\n        wg.Wait()\n        close(out)\n    }()\n\n    return out\n}\n\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n\n    \u002F\u002F Generate input\n    in := generate(ctx, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n\n    \u002F\u002F Fan out to multiple squarers\n    c1 := square(ctx, in)\n    c2 := square(ctx, in)\n    c3 := square(ctx, in)\n\n    \u002F\u002F Fan in results\n    for result := range merge(ctx, c1, c2, c3) {\n        fmt.Println(result)\n    }\n}\n```\n\n### Pattern 3: Bounded Concurrency with Semaphore\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"golang.org\u002Fx\u002Fsync\u002Fsemaphore\"\n    \"sync\"\n)\n\ntype RateLimitedWorker struct {\n    sem *semaphore.Weighted\n}\n\nfunc NewRateLimitedWorker(maxConcurrent int64) *RateLimitedWorker {\n    return &RateLimitedWorker{\n        sem: semaphore.NewWeighted(maxConcurrent),\n    }\n}\n\nfunc (w *RateLimitedWorker) Do(ctx context.Context, tasks []func() error) []error {\n    var (\n        wg     sync.WaitGroup\n        mu     sync.Mutex\n        errors []error\n    )\n\n    for _, task := range tasks {\n        \u002F\u002F Acquire semaphore (blocks if at limit)\n        if err := w.sem.Acquire(ctx, 1); err != nil {\n            return []error{err}\n        }\n\n        wg.Add(1)\n        go func(t func() error) {\n            defer wg.Done()\n            defer w.sem.Release(1)\n\n            if err := t(); err != nil {\n                mu.Lock()\n                errors = append(errors, err)\n                mu.Unlock()\n            }\n        }(task)\n    }\n\n    wg.Wait()\n    return errors\n}\n\n\u002F\u002F Alternative: Channel-based semaphore\ntype Semaphore chan struct{}\n\nfunc NewSemaphore(n int) Semaphore {\n    return make(chan struct{}, n)\n}\n\nfunc (s Semaphore) Acquire() {\n    s \u003C- struct{}{}\n}\n\nfunc (s Semaphore) Release() {\n    \u003C-s\n}\n```\n\n### Pattern 4: Graceful Shutdown\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"os\"\n    \"os\u002Fsignal\"\n    \"sync\"\n    \"syscall\"\n    \"time\"\n)\n\ntype Server struct {\n    shutdown chan struct{}\n    wg       sync.WaitGroup\n}\n\nfunc NewServer() *Server {\n    return &Server{\n        shutdown: make(chan struct{}),\n    }\n}\n\nfunc (s *Server) Start(ctx context.Context) {\n    \u002F\u002F Start workers\n    for i := 0; i \u003C 5; i++ {\n        s.wg.Add(1)\n        go s.worker(ctx, i)\n    }\n}\n\nfunc (s *Server) worker(ctx context.Context, id int) {\n    defer s.wg.Done()\n    defer fmt.Printf(\"Worker %d stopped\\n\", id)\n\n    ticker := time.NewTicker(time.Second)\n    defer ticker.Stop()\n\n    for {\n        select {\n        case \u003C-ctx.Done():\n            \u002F\u002F Cleanup\n            fmt.Printf(\"Worker %d cleaning up...\\n\", id)\n            time.Sleep(500 * time.Millisecond) \u002F\u002F Simulated cleanup\n            return\n        case \u003C-ticker.C:\n            fmt.Printf(\"Worker %d working...\\n\", id)\n        }\n    }\n}\n\nfunc (s *Server) Shutdown(timeout time.Duration) {\n    \u002F\u002F Signal shutdown\n    close(s.shutdown)\n\n    \u002F\u002F Wait with timeout\n    done := make(chan struct{})\n    go func() {\n        s.wg.Wait()\n        close(done)\n    }()\n\n    select {\n    case \u003C-done:\n        fmt.Println(\"Clean shutdown completed\")\n    case \u003C-time.After(timeout):\n        fmt.Println(\"Shutdown timed out, forcing exit\")\n    }\n}\n\nfunc main() {\n    \u002F\u002F Setup signal handling\n    ctx, cancel := context.WithCancel(context.Background())\n\n    sigCh := make(chan os.Signal, 1)\n    signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)\n\n    server := NewServer()\n    server.Start(ctx)\n\n    \u002F\u002F Wait for signal\n    sig := \u003C-sigCh\n    fmt.Printf(\"\\nReceived signal: %v\\n\", sig)\n\n    \u002F\u002F Cancel context to stop workers\n    cancel()\n\n    \u002F\u002F Wait for graceful shutdown\n    server.Shutdown(5 * time.Second)\n}\n```\n\n### Pattern 5: Error Group with Cancellation\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"golang.org\u002Fx\u002Fsync\u002Ferrgroup\"\n    \"net\u002Fhttp\"\n)\n\nfunc fetchAllURLs(ctx context.Context, urls []string) ([]string, error) {\n    g, ctx := errgroup.WithContext(ctx)\n\n    results := make([]string, len(urls))\n\n    for i, url := range urls {\n        i, url := i, url \u002F\u002F Capture loop variables\n\n        g.Go(func() error {\n            req, err := http.NewRequestWithContext(ctx, \"GET\", url, nil)\n            if err != nil {\n                return fmt.Errorf(\"creating request for %s: %w\", url, err)\n            }\n\n            resp, err := http.DefaultClient.Do(req)\n            if err != nil {\n                return fmt.Errorf(\"fetching %s: %w\", url, err)\n            }\n            defer resp.Body.Close()\n\n            results[i] = fmt.Sprintf(\"%s: %d\", url, resp.StatusCode)\n            return nil\n        })\n    }\n\n    \u002F\u002F Wait for all goroutines to complete or one to fail\n    if err := g.Wait(); err != nil {\n        return nil, err \u002F\u002F First error cancels all others\n    }\n\n    return results, nil\n}\n\n\u002F\u002F With concurrency limit\nfunc fetchWithLimit(ctx context.Context, urls []string, limit int) ([]string, error) {\n    g, ctx := errgroup.WithContext(ctx)\n    g.SetLimit(limit) \u002F\u002F Max concurrent goroutines\n\n    results := make([]string, len(urls))\n    var mu sync.Mutex\n\n    for i, url := range urls {\n        i, url := i, url\n\n        g.Go(func() error {\n            result, err := fetchURL(ctx, url)\n            if err != nil {\n                return err\n            }\n\n            mu.Lock()\n            results[i] = result\n            mu.Unlock()\n            return nil\n        })\n    }\n\n    if err := g.Wait(); err != nil {\n        return nil, err\n    }\n\n    return results, nil\n}\n```\n\n### Pattern 6: Concurrent Map with sync.Map\n\n```go\npackage main\n\nimport (\n    \"sync\"\n)\n\n\u002F\u002F For frequent reads, infrequent writes\ntype Cache struct {\n    m sync.Map\n}\n\nfunc (c *Cache) Get(key string) (interface{}, bool) {\n    return c.m.Load(key)\n}\n\nfunc (c *Cache) Set(key string, value interface{}) {\n    c.m.Store(key, value)\n}\n\nfunc (c *Cache) GetOrSet(key string, value interface{}) (interface{}, bool) {\n    return c.m.LoadOrStore(key, value)\n}\n\nfunc (c *Cache) Delete(key string) {\n    c.m.Delete(key)\n}\n\n\u002F\u002F For write-heavy workloads, use sharded map\ntype ShardedMap struct {\n    shards    []*shard\n    numShards int\n}\n\ntype shard struct {\n    sync.RWMutex\n    data map[string]interface{}\n}\n\nfunc NewShardedMap(numShards int) *ShardedMap {\n    m := &ShardedMap{\n        shards:    make([]*shard, numShards),\n        numShards: numShards,\n    }\n    for i := range m.shards {\n        m.shards[i] = &shard{data: make(map[string]interface{})}\n    }\n    return m\n}\n\nfunc (m *ShardedMap) getShard(key string) *shard {\n    \u002F\u002F Simple hash\n    h := 0\n    for _, c := range key {\n        h = 31*h + int(c)\n    }\n    return m.shards[h%m.numShards]\n}\n\nfunc (m *ShardedMap) Get(key string) (interface{}, bool) {\n    shard := m.getShard(key)\n    shard.RLock()\n    defer shard.RUnlock()\n    v, ok := shard.data[key]\n    return v, ok\n}\n\nfunc (m *ShardedMap) Set(key string, value interface{}) {\n    shard := m.getShard(key)\n    shard.Lock()\n    defer shard.Unlock()\n    shard.data[key] = value\n}\n```\n\n### Pattern 7: Select with Timeout and Default\n\n```go\nfunc selectPatterns() {\n    ch := make(chan int)\n\n    \u002F\u002F Timeout pattern\n    select {\n    case v := \u003C-ch:\n        fmt.Println(\"Received:\", v)\n    case \u003C-time.After(time.Second):\n        fmt.Println(\"Timeout!\")\n    }\n\n    \u002F\u002F Non-blocking send\u002Freceive\n    select {\n    case ch \u003C- 42:\n        fmt.Println(\"Sent\")\n    default:\n        fmt.Println(\"Channel full, skipping\")\n    }\n\n    \u002F\u002F Priority select (check high priority first)\n    highPriority := make(chan int)\n    lowPriority := make(chan int)\n\n    for {\n        select {\n        case msg := \u003C-highPriority:\n            fmt.Println(\"High priority:\", msg)\n        default:\n            select {\n            case msg := \u003C-highPriority:\n                fmt.Println(\"High priority:\", msg)\n            case msg := \u003C-lowPriority:\n                fmt.Println(\"Low priority:\", msg)\n            }\n        }\n    }\n}\n```\n\n## Race Detection\n\n```bash\n# Run tests with race detector\ngo test -race .\u002F...\n\n# Build with race detector\ngo build -race .\n\n# Run with race detector\ngo run -race main.go\n```\n\n## Best Practices\n\n### Do's\n- **Use context** - For cancellation and deadlines\n- **Close channels** - From sender side only\n- **Use errgroup** - For concurrent operations with errors\n- **Buffer channels** - When you know the count\n- **Prefer channels** - Over mutexes when possible\n\n### Don'ts\n- **Don't leak goroutines** - Always have exit path\n- **Don't close from receiver** - Causes panic\n- **Don't use shared memory** - Unless necessary\n- **Don't ignore context cancellation** - Check ctx.Done()\n- **Don't use time.Sleep for sync** - Use proper primitives\n\n## Resources\n\n- [Go Concurrency Patterns](https:\u002F\u002Fgo.dev\u002Fblog\u002Fpipelines)\n- [Effective Go - Concurrency](https:\u002F\u002Fgo.dev\u002Fdoc\u002Feffective_go#concurrency)\n- [Go by Example - Goroutines](https:\u002F\u002Fgobyexample.com\u002Fgoroutines)\n",{"name":5,"description":7},"main","7850740bd837adb32f0a109ab2af82482196c6f2","skills\u002Fwshobson-agents\u002Fplugins\u002Fsystems-programming\u002Fskills\u002Fgo-concurrency-patterns",1,"https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills","https:\u002F\u002Fzturnsgo.com","https:\u002F\u002Fgithub.com\u002Fshengdabai","https:\u002F\u002Fzturnsgo.com\u002F","AI × education creator — producer, not consumer. Taught 6,000+ learners; shipping learning tools & daily essays with Claude Code, practicing @xiaolai's way 🥋","2026-11-01 21:28:23",{"source":8,"skillName":5,"commands":54,"prompt":64,"zipAvailable":65},[55,58,61],{"tool":56,"command":57},"npx","npx skills add shengdabai\u002FTony-Claude-Code-Skills --skill go-concurrency-patterns",{"tool":59,"command":60},"bunx","bunx skills add shengdabai\u002FTony-Claude-Code-Skills --skill go-concurrency-patterns",{"tool":62,"command":63},"pnpm","pnpm dlx skills add shengdabai\u002FTony-Claude-Code-Skills --skill go-concurrency-patterns","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fwshobson-agents\u002Fplugins\u002Fsystems-programming\u002Fskills\u002Fgo-concurrency-patterns\u002FSKILL.md",true,[],[68,77,85,93,101,109],{"skillNo":69,"name":70,"nameZh":6,"description":71,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":72,"sourceHtmlUrl":73,"categories":74,"keywords":75,"publishedAt":76},"01M73H7XKBHE1ETQDZT94MF9HH","seedance-prompt-en","Write effective prompts for Jimeng Seedance 2.0 multimodal AI video generation. Use when users want to create video prompts using text, images, videos, and audio inputs with the @ reference system. Covers camera movements, effects replication, video extension, editing, music beat-matching, e-commerce ads, short dramas, and educational content.","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fseedance-prompt-en\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fseedance-prompt-en\u002FSKILL.md",[],[],"2026-11-11 15:11:14",{"skillNo":78,"name":79,"nameZh":6,"description":80,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":81,"sourceHtmlUrl":82,"categories":83,"keywords":84,"publishedAt":76},"01M73GRE9998Q1DZWC71N41JWG","onepage-pdf","Convert an HTML page into a single continuous-page PDF (one tall page, no pagination breaks), preserving desktop layout, backgrounds and selectable text. Use when the user asks to turn an HTML file\u002Freport\u002Fproposal into a \"single-page PDF\", \"long PDF\", \"不分页 PDF\", \"单页 PDF\", \"长图式 PDF\", or complains that an HTML-to-PDF export breaks into pages or cuts content. Supports optional string redaction with leak verification.","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fonepage-pdf\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fonepage-pdf\u002FSKILL.md",[],[],{"skillNo":86,"name":87,"nameZh":6,"description":88,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":89,"sourceHtmlUrl":90,"categories":91,"keywords":92,"publishedAt":76},"01M73GQG1KZDADVPV3MHAT1J5A","native-feel-cross-platform-desktop","Use when the user is designing, prototyping, or rewriting a desktop app that must run on multiple OSes (macOS + Windows, optionally Linux) AND feel indistinguishable from a native app to its users — fast launch, native windowing, native input handling, native materials. Trigger words include \"cross-platform desktop\", \"Electron alternative\", \"Tauri vs native\", \"WebView wrapper\", \"near-native performance\", \"Raycast architecture\", \"WebKit\u002FWebView2 quirks\", \"WKWebView\", \"system tray app\", \"global hotkey app\", \"launcher app\". Do NOT trigger this skill for pure web apps, pure mobile apps, or for greenfield projects that have no native-feel requirement.","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fnative-feel-cross-platform-desktop\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fnative-feel-cross-platform-desktop\u002FSKILL.md",[],[],{"skillNo":94,"name":95,"nameZh":6,"description":96,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":97,"sourceHtmlUrl":98,"categories":99,"keywords":100,"publishedAt":76},"01M73G87PDNSMKG2FHWCYER6N8","gstack","Router for the gstack skill suite. (gstack)","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fgstack\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fgstack\u002FSKILL.md",[],[],{"skillNo":102,"name":103,"nameZh":6,"description":104,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":105,"sourceHtmlUrl":106,"categories":107,"keywords":108,"publishedAt":76},"01M73G3EAQWKCM8DB8D0N0QSVG","fireworks-tech-graph","Use when the user wants to create any technical diagram - architecture, data flow, flowchart, sequence, agent\u002Fmemory, or concept map - and export as SVG+PNG. Trigger on: \"画图\" \"帮我画\" \"生成图\" \"做个图\" \"架构图\" \"流程图\" \"可视化一下\" \"出图\" \"generate diagram\" \"draw diagram\" \"visualize\" or any system\u002Fflow description the user wants illustrated.","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Ffireworks-tech-graph\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Ffireworks-tech-graph\u002FSKILL.md",[],[],{"skillNo":110,"name":111,"nameZh":6,"description":112,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":113,"categories":114,"keywords":115,"publishedAt":76},"01M73G086AXAH9DW1H5G5N3CPB","dbs-video-extract","接收抖音、小红书、微信视频号等短视频链接或分享文案，查询作品／账号数据并提取语音文字稿，按作者和标题归档为 Markdown。用户希望完整解析短视频、取得数据或文字稿时使用；运行前检查 TikHub 与轻抖使用凭证，缺少时用新手能理解的方式引导购买和安全配置。","https:\u002F\u002Fgithub.com\u002Fshengdabai\u002FTony-Claude-Code-Skills\u002Fblob\u002Fmain\u002Fskills\u002Fdbs-video-extract\u002FSKILL.md",[],[]]