# Golang Concurrency

> Standards for safe concurrent programming using Goroutines, Channels, and Context. Use when implementing concurrency with Goroutines, Channels, or Context in Go. (triggers: goroutine, go keyword, channel, mutex, waitgroup, context, errgroup, race condition)

- Skill: `hoangnguyen0403/golang-concurrency-2` (Agent Skill)
- Install (CLI): `npx skillmds@latest add hoangnguyen0403/golang-concurrency-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hoangnguyen0403/golang-concurrency-2/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: HoangNguyen0403 (https://skillmd.com/u/hoangnguyen0403)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/hoangnguyen0403/golang-concurrency-2

---


# Golang Concurrency Standards

## **Priority: P0 (CRITICAL)**

## Principles

- **Share Memory by Communicating**: Don't communicate by sharing memory. Use channels.
- **Context is King**: Always pass `ctx` to efficient manage cancellation/timeouts.
- **Prevent Leaks**: Never start a goroutine without knowing how it will stop.
- **Race Detection**: Always run tests with `go test -race`.

## Primitives

- **Goroutines**: Lightweight threads. `go func() { ... }()`
- **Channels**: For data passing + synchronization.
- **WaitGroup**: Wait for a group of goroutines to finish.
- **ErrGroup**: WaitGroup + Error propagation (Preferred).
- **Mutex**: Protect shared state (simpler than channels for just state).

## Guidelines

- **Buffered vs Unbuffered**: Use unbuffered channels for strict synchronization. Use buffered only if you specifically need async decoupling/burst handling.
- **Closed Channels**: Panic if writing to closed. Reading from closed returns zero-value immediately.
- **Select**: Use `select` to handle multiple channels or timeouts.

## References

- [Concurrency Patterns](references/concurrency-patterns.md)
- [Context Usage](references/context-usage.md)


## Anti-Patterns

- **No goroutine leaks**: Every goroutine needs a known exit path.
- **No global state mutation across goroutines**: Use channels or sync primitives.
- **No bare goroutines without lifecycle**: Use errgroup or WaitGroup.


