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

# README

The Story

Imagine a piggy bank named "money." Two siblings, Stingy and Spendy, are constantly fighting over it.

  • Stingy: Wants to add money to the piggy bank (10 coins at a time).
  • Spendy: Wants to take money from the piggy bank (also 10 coins at a time).

Because they don't want to get into a real fight, they agree to take turns. Every time they add or remove coins, they politely tell the other sibling to have a go.

The Go Code

package main 
// This is like saying, "We're starting a new game!"

import (
  "fmt"      // For printing messages 
  "runtime"   // For telling the siblings to take turns
  "time"      // For pausing the game for a bit
)

// ... (the rest of the code is the same as the original)

How It Works

  1. Setting the Stage:

    • money := 100: We start with 100 coins in the piggy bank.
    • go stingy(&money): We tell Stingy to start adding coins (but in the background, like on a separate play mat).
    • go spendy(&money): We tell Spendy to start taking coins (also in the background).
  2. Taking Turns:

    • Inside stingy and spendy:
      • The for loop makes them repeat their actions a million times.
      • *money += 10 (Stingy adds 10 coins).
      • *money -= 10 (Spendy removes 10 coins).
      • runtime.Gosched(): They say, "Your turn!" and let the other sibling have a go.
  3. Time Out:

    • time.Sleep(2 * time.Second): The game pauses for 2 seconds.
  4. Counting the Money:

    • fmt.Print("Money left: ", money): We print how many coins are left in the piggy bank.

Why Think This Way?

  • Goroutines (the go keyword): Imagine these are like having multiple play areas where siblings can do their actions at the same time.
  • Pointers (&money): Think of this like the siblings sharing the same piggy bank, not copies.
  • Gosched(): This is like the siblings taking turns so they don't cause a huge mess!

Important Note: In this example, the final amount of money will be unpredictable. Because Stingy and Spendy are working at the same time, they might try to change the money simultaneously, leading to unexpected results.