package
0.0.0-20240731084147-8c2c48fecfe2
Repository: https://github.com/jimsyyap/golang_recipe.git
Documentation: pkg.go.dev

# README

What the Code Does

Imagine you have a team of workers who need to complete tasks. You want to keep track of when each worker starts and finishes their task, and you want to know when all the work is done. This code does just that, but in the world of computer programs.

  1. The Worker (doWork)

    • Each worker (goroutine) gets a number (id).
    • It announces that it has started its work and the current time.
    • It pretends to work for 2 seconds (using time.Sleep).
    • It announces that it has finished its work.
    • Finally, it sends a message ("Work [id] completed") through a channel. Think of the channel like a walkie-talkie for communication.
  2. The Coordinator (main)

    • We have a team of 15 workers (numWorkers).
    • We create a special tool called a "WaitGroup" (wg). This helps us keep track of whether all workers have finished.
    • We also create a "channel" (ch). This is like a message board where workers can post their completion messages.
    • We tell each worker to start their task. They all work concurrently (at roughly the same time).
    • The main program checks the message board (ch) and prints each worker's completion message as it arrives.
    • The main program uses the WaitGroup (wg) to wait patiently until all workers have finished.
    • Finally, the main program announces that all the work is done.

Thought Process: How to Write Concurrent Code

  1. Identify Tasks: What are the individual units of work that can be done independently? In this case, each worker's task is simulated by the doWork function.
  2. Concurrency: Use the go keyword to make each task run in its own "goroutine" (like a lightweight thread).
  3. Synchronization:
    • WaitGroup: Use a WaitGroup to ensure the main program doesn't end before all tasks are complete. This is important when tasks take different amounts of time.
    • Channel: Use a channel to communicate messages between goroutines. In this case, it's used to signal task completion.

Why This Matters

This code demonstrates how to manage and coordinate multiple tasks running concurrently in a Go program. It's a simple example of using goroutines, WaitGroups, and channels, which are fundamental tools for building efficient and scalable concurrent applications.