Skip to content
Skillpedia
General coding41k · #6798

go-concurrency-patterns

What it does

This tool helps you understand and use advanced ways to do multiple things at the same time in a Go computer program. It teaches you how to use 'goroutines' (small tasks that run together) and 'channels' (communication lines between these tasks).

When to use it

  • ✓When building Go programs that need to do multiple things concurrently.
  • ✓When organizing tasks to work together efficiently (like in 'worker pools').
  • ✓When trying to figure out why your program is crashing or having concurrency errors.

Skill link

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

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/go-concurrency-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“How can I make two parts of my Go program run at the same time and exchange information safely?”

5 new skills a week. 1 minute to read.

How it works

It explains the basic building blocks of concurrency in Go, like 'goroutines' and 'channels', and shows examples of how to use them for communication and synchronization.

Technical content (original SKILL.md)

Original text by the author, straight from 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
FAQ

What is a 'goroutine'?

It's like a function that can run at the same time as other parts of your program, without interfering.

What are 'channels' for?

They are like pipes through which 'goroutines' can send and receive messages to each other safely.

What does 'Don't communicate by sharing memory; share memory by communicating' mean?

It's advice to avoid having multiple parts of your program access the same data at the same time (which can cause errors). Instead, use 'channels' to pass data between them.

Details

Original description by the author

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

Popularity

GitHub stars
41k
Ranking position
#6798
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills