Skip to main content

Go Concurrency Patterns Cookbook

Implement Go concurrency patterns: worker pools, fan-out/fan-in, pipelines, context cancellation, backpressure, and race-free shutdown.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt implements a specific Go concurrency pattern for a real use case rather than a textbook toy. You set [pattern_name] such as Worker Pool, Fan-Out/Fan-In, or Pipeline, and [use_case], and the AI produces a runnable main.go with a goroutine-lifecycle ASCII diagram, an implementation using the [concurrency_primitives] you choose, context propagation for cancellation and timeouts, and errgroup-based error handling across goroutines. It front-loads exactly the parts where concurrency bugs hide, instead of just spawning goroutines and hoping nothing leaks.

The variables tune the implementation to your workload. [concurrency_primitives] picks the building blocks like channels, sync.WaitGroup, context, and errgroup, [buffer_size] sets the backpressure behavior of buffered channels, and [benchmark_size] drives a sequential-versus-concurrent benchmark so you can see the actual speedup rather than assuming one. The prompt also requires graceful shutdown that drains in-flight work, lightweight metrics covering goroutine count, processing rate, and queue depth, race-detector cleanliness, and production notes on max-goroutine limits and deadlock prevention.

When to use it

  • Building a Go service that processes large batches against a rate-limited external API
  • Implementing a worker pool or pipeline with correct, drain-on-shutdown semantics
  • Adding context-based cancellation and timeouts to existing concurrent work
  • Getting backpressure right with buffered channels under sustained load
  • Benchmarking concurrent versus sequential throughput before committing to a design
  • Hardening concurrent code so it passes the race detector cleanly before shipping

Example output

You get a full, runnable main.go with comments explaining each decision: an ASCII goroutine-lifecycle diagram, the chosen pattern implemented with your primitives, context propagation throughout, errgroup error handling, and a graceful drain-on-shutdown path. It includes buffered-channel backpressure sized by [buffer_size], lightweight metrics, a benchmark comparing sequential and concurrent runs over your [benchmark_size] dataset, and production notes on goroutine limits, memory usage, and deadlock avoidance.

Pro tips

  • Match [pattern_name] to the real shape of your work; a pipeline and a worker pool solve genuinely different problems
  • Describe [use_case] concretely, including any rate limits, so the implementation reflects your real constraints
  • Tune [buffer_size] to your throughput; too large hides backpressure, too small starves the workers and stalls
  • Always run the result under the race detector before trusting it, even when the logic looks obviously correct
  • Treat the graceful-drain path as non-negotiable; that is precisely where lost work and goroutine leaks hide
  • Wire the metrics on goroutine count and queue depth so you can see backpressure building rather than guessing
  • Set a sensible max-goroutine limit in production so a sudden burst of input cannot exhaust memory and crash the process
  • Use a [benchmark_size] close to your real volume so the reported speedup numbers actually mean something in practice

Frequently Asked Questions

Which concurrency patterns does this support?
The `[pattern_name]` variable covers common patterns like Worker Pool, Fan-Out/Fan-In, and Pipeline. Match the pattern to the shape of your work, since a worker pool, a pipeline, and fan-out/fan-in solve genuinely different work-distribution problems with different trade-offs.
Does it handle graceful shutdown correctly?
Yes. The prompt explicitly requires draining in-flight work before exit and context-based cancellation throughout. This matters because abrupt shutdown is where goroutine leaks and lost work hide, so treat the drain path as a non-negotiable part of the output.
Will the generated code pass the race detector?
The prompt is designed to produce race-free code and instructs running with -race. Still, always run the result under the race detector yourself before trusting it; subtle data races can survive code review even when the logic looks completely correct.
How do I tune the channel buffer size?
Set `[buffer_size]` based on your throughput and memory budget. Too large a buffer hides backpressure and lets work pile up unboundedly; too small starves workers and stalls the pipeline. Benchmark with your real volume to find the right balance.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Rust & Go Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support