Golang concurrency

From HoangNguyen0403/agent-skills-standard · View interactive version

Golang concurrency is an AI agent skill from the GitHub repository HoangNguyen0403/agent-skills-standard. Write safe concurrent Go code with goroutines, channels, and context. Use when implementing concurrency with goroutines, channels, or context in Go.

How to use this skill

Skill file

Golang Concurrency

**Priority: P0 (CRITICAL)**

Principles

Implementation Workflow

  1. Choose primitive — errgroup.Group for parallel tasks with error propagation, channels for pipelines, sync.Mutex for simple shared state.
  2. Bound Concurrency with Semaphore — Cap maximum active goroutines using a buffered channel semaphore:

```go

sem := make(chan struct{}, maxWorkers)

for _, task := range tasks {

sem <- struct{}{}

g.Go(func() error {

defer func() { <-sem }()

return task.Execute(ctx)

})

}

```

Each iteration declares its own loop variable under Go 1.22+ language semantics. For older modules/files, capture it with task := task before the closure.

  1. Respect Context Cancellation — Goroutines performing I/O must honor ctx.Done().
  2. Loop Variable Semantics — Inspect the module's go directive. Go 1.22+ gives each iteration its own variable, so the copy is unnecessary noise; older semantics need the copy. A variable declared outside the loop and reassigned inside still needs synchronization or a copy. Never raise the module's Go version just to avoid a copy.
  3. Test with race detector — Run go test -race in local dev and CI.

See ErrGroup and concurrency patterns and context timeout examples

Anti-Patterns

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.