Implement goroutines, channels, and concurrency patterns
✓Works with OpenClaudeYou are a Go concurrency expert. The user wants to implement goroutines, channels, and concurrency patterns efficiently and safely.
What to check first
- Run
go versionto verify Go 1.18+ is installed (generics support for advanced patterns) - Check if you're using buffered or unbuffered channels — this determines blocking behavior
- Verify context package imports for cancellation and timeouts
Steps
- Create a goroutine using
go functionName()syntax — this spawns a lightweight thread managed by the Go runtime - Define a channel type with
make(chan Type)ormake(chan Type, bufferSize)for buffered channels - Send data to a channel with
channel <- valueand receive withvalue := <-channel - Use
selectstatement to multiplex on multiple channels withcase <-channelA:andcase <-channelB: - Implement worker pools by launching N goroutines that read from a shared work channel
- Add context cancellation with
ctx, cancel := context.WithCancel(context.Background())to all goroutines - Use
sync.WaitGroupto track goroutine completion with.Add(),.Done(), and.Wait()methods - Guard shared state with
sync.Mutexusing.Lock()and.Unlock()around critical sections
Code
package main
import (
"context"
"fmt"
"sync"
"time"
)
// Worker pool pattern with channels and goroutines
func workerPool(jobs <-chan int, results chan<- string, workerID int) {
for job := range jobs {
result := fmt.Sprintf("Worker %d processed job %d", workerID, job)
results <- result
time.Sleep(100 * time.Millisecond)
}
}
// Fan-out/fan-in pattern
func merge(ctx context.Context, channels ...<-chan string) <-chan string {
var wg sync.WaitGroup
out := make(chan string)
output := func(ch <-chan string) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case val, ok := <-ch:
if !ok {
return
}
select {
case out <- val:
case <-ctx.Done():
return
}
}
}
}
wg.Add(len(channels))
for _, ch := range channels {
go output(ch)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
// Rate limiter using time.Ticker
func rateLimiter(limit int) <-chan time.Time {
return time.Tick(time.Second / time.Duration(limit))
}
func
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Go Skills
Other Claude Code skills in the same category — free to download.
Go API Setup
Scaffold Go REST API with Gin or Chi router
Go Testing
Set up Go testing with table-driven tests and mocks
Go Modules
Manage Go modules and dependency versioning
Go Docker
Create optimized multi-stage Docker build for Go apps
Go gRPC
Set up gRPC server and client in Go
Go Middleware
Create HTTP middleware chain in Go
Go CLI
Build CLI applications with Cobra in Go
Want a Go skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.