Goroutines

Back

Loading concept...

🚀 Goroutines: Your Army of Tiny Helpers

The Kitchen Analogy

Imagine you’re making a big breakfast. You need to:

  • Fry eggs
  • Make toast
  • Brew coffee

Without goroutines: You fry eggs, wait until done. Then make toast, wait. Then brew coffee, wait. Takes forever!

With goroutines: You start all three at once! Like having tiny kitchen helpers who each handle one task. Everything finishes faster.

That’s exactly what goroutines do in Go. They’re your tiny helpers that work on tasks at the same time.


🔑 The go Keyword: The Magic Word

The go keyword is like saying “Hey helper, go do this for me!”

Without go (One thing at a time)

func main() {
    cookEggs()   // Wait...
    makeToast()  // Wait...
    brewCoffee() // Wait...
}

With go (All at once!)

func main() {
    go cookEggs()   // Helper 1 starts!
    go makeToast()  // Helper 2 starts!
    go brewCoffee() // Helper 3 starts!
}

Key Point: Just add go before any function call. That’s it!

graph TD A["Your Program"] -->|go| B["Helper 1: cookEggs"] A -->|go| C["Helper 2: makeToast"] A -->|go| D["Helper 3: brewCoffee"] B --> E["All Done!"] C --> E D --> E

🎨 Creating Goroutines: Three Easy Ways

Way 1: Call an Existing Function

func sayHello() {
    fmt.Println("Hello!")
}

func main() {
    go sayHello() // Creates helper
}

Way 2: Use an Anonymous Function

Like a one-time helper with specific instructions.

func main() {
    go func() {
        fmt.Println("I'm anonymous!")
    }() // Don't forget these ()
}

Way 3: Pass Data to Your Helper

func greet(name string) {
    fmt.Println("Hi", name)
}

func main() {
    go greet("Alice") // Helper says hi
    go greet("Bob")   // Another helper
}

Remember: Each go creates a new helper. You can have thousands of them!


🌱 Goroutine Lifecycle: Birth to End

Every goroutine has a simple life:

graph TD A["🐣 BORN: go keyword called"] --> B["⏳ WAITING: Might wait for turn"] B --> C["🏃 RUNNING: Doing the work"] C --> D{More work?} D -->|Yes| B D -->|No| E["💀 DONE: Function ends"]

Stage 1: Born

go doSomething() // 🐣 Goroutine is born!

The moment you write go, a new goroutine starts.

Stage 2: Running or Waiting

Go has a smart scheduler. It decides which helper works when. Your helpers might:

  • Run right away
  • Wait their turn (if CPU is busy)

Stage 3: Done

When the function finishes, the goroutine disappears. No cleanup needed!

func quickTask() {
    fmt.Println("Done!")
    // Goroutine ends here automatically
}

⚠️ The Disappearing Main Problem

Here’s a tricky part. Watch this:

func main() {
    go fmt.Println("Hello!")
    // Program ends immediately!
}

Output: Nothing! Why?

The main function is like a parent. When the parent leaves, all helpers stop!

The Fix: Wait a Moment

func main() {
    go fmt.Println("Hello!")
    time.Sleep(time.Second) // Wait 1 sec
}

Output: Hello!

Later, you’ll learn better ways to wait (channels, WaitGroup). For now, time.Sleep works!


🎮 Real Example: Counting Together

func count(name string) {
    for i := 1; i <= 3; i++ {
        fmt.Println(name, ":", i)
        time.Sleep(500 * time.Millisecond)
    }
}

func main() {
    go count("Helper A")
    go count("Helper B")

    time.Sleep(2 * time.Second)
}

Possible Output:

Helper A : 1
Helper B : 1
Helper A : 2
Helper B : 2
Helper A : 3
Helper B : 3

See how they alternate? Both working at the same time!


💡 Quick Tips

Do This ✅ Not This ❌
go myFunc() go myFunc (missing ())
Keep main alive Let main exit early
Create many goroutines Worry about “too many”

🎯 Summary

  1. go keyword = Start a new helper
  2. Goroutine creation = Add go before any function
  3. Lifecycle = Born → Running/Waiting → Done

Remember the kitchen: Every go is like hiring a new cook. They all work together, making everything faster!


🏆 You Did It!

Now you understand:

  • ✅ What the go keyword does
  • ✅ How to create goroutines
  • ✅ The goroutine lifecycle

You’re ready to make your Go programs concurrent! 🎉

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.