Go Concurrency
Project workbook ยท Path 82

Create a bounded Go worker-pool example that processes four jobs and collects every result without depending on completion order.

The case

Four synthetic jobs contain values 2, 3, 4 and 5. Two workers square each value. Completion order is unspecified; the final report sorts by job ID. Each submitted job must produce exactly one result.

Job IDInputExpected square
124
239
3416
4525
STAGE 1

Review Go values and functions

Start with a sequential function that maps one job to one result. Keep the transformation deterministic so concurrency can be tested against a simple baseline. Use a struct with an ID instead of relying on slice position after results arrive. The case does no external work and has no sensitive input.

Your task

Write job and result types plus the square function.

Required output

Sequential baseline

Record your result and evidence here or in a separate file.

Review gate

The four expected values are 4, 9, 16 and 25.

If the gate is not met

Verify the sequential result before introducing goroutines.

STAGE 2

Bound the worker pool

A fixed number of workers limits simultaneous processing. Workers read jobs until the jobs channel is closed. The producer closes that channel after sending every job. Avoid creating a goroutine for every unbounded input without considering resource use; this exercise intentionally uses exactly two workers.

Your task

Implement two workers consuming a jobs channel.

Required output

Worker-pool source

Record your result and evidence here or in a separate file.

Review gate

Every job is submitted once and processed once.

If the gate is not met

Track job IDs rather than counting log messages.

STAGE 3

Coordinate completion

Use a wait group to know when all workers have finished sending results. A separate coordinator closes the results channel only then. Closing it from an individual worker can make another worker send to a closed channel. Collect results concurrently so a small channel buffer does not cause a deadlock.

Your task

Add completion coordination and range over the result channel.

Required output

Completion-safe collector

Record your result and evidence here or in a separate file.

Review gate

Collection terminates after all four results without an arbitrary sleep.

If the gate is not met

Use synchronization rather than a guessed waiting time.

STAGE 4

Test order-independent correctness

Compare results by ID, not arrival order. Check for missing and duplicate IDs as well as correct values. A race detector can help inspect a real run, but it is not available here because the preparation environment lacks Go. The package includes source and local commands without claiming they executed.

Your task

Run go test and the race check locally when available.

Required output

Concurrency test record

Record your result and evidence here or in a separate file.

Review gate

All four IDs appear once; ordering differences do not fail a valid run.

If the gate is not met

Sort or map results before comparison.

STAGE 5

Explain limits and shutdown

A finite fixture can finish without cancellation, but a real service needs timeouts, cancellation and error handling. State those gaps rather than adding an untested production wrapper. Explain channel ownership and what would need to change for slow or failing jobs. The example is educational and does not promise throughput gains.

Your task

Write a channel-ownership diagram and an extension plan.

Required output

Go handoff

Record your result and evidence here or in a separate file.

Review gate

The README identifies producer, workers and results closer.

If the gate is not met

Name one owner for each close operation.

Final project

Deliver the sequential baseline, worker pool, result checks and channel-ownership explanation. Add an empty-job case and demonstrate clean termination in a local Go environment.

Critical condition

No worker closes the shared results channel prematurely and every job ID has exactly one result.

CriterionPointsEvidence
Evidence and correctness30Calculations, source use and results match the supplied case; limitations are honest.
Required artifacts30All five stage outputs and the capstone handoff are present and inspectable.
Reasoning and decisions20Choices, assumptions, alternatives and the case-specific critical condition are explained.
Reproducibility10A reviewer can follow the stated steps and distinguish expected from observed results.
Clear communication10Labels, units, structure and evidence references make the work easy to review.

Score at least 80/100, supply every required artifact, and satisfy the path-specific critical condition. Rework missing evidence before marking complete.

Worked reference

The result set is {1:4, 2:9, 3:16, 4:25}; its value sum is 54. Arrival order may vary. The source uses two workers and a completion coordinator. Go compilation, runtime and race-detector checks were not performed in the preparation environment.

Boundaries

This is a finite local concurrency example, not a resilient task queue or a benchmarked service.