Fundamentals 8 min read

Mastering Go Concurrency: How Goroutines Simplify Parallel Programming

Go’s goroutine model lets developers replace complex thread‑pool management with lightweight, runtime‑scheduled functions, enabling simple concurrent execution; this article explains how to launch single or multiple goroutines, synchronize them with WaitGroup, and understand the underlying G‑P‑M scheduler.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Go Concurrency: How Goroutines Simplify Parallel Programming

When implementing concurrent programming in Java or C++, developers often have to maintain their own thread pools, wrap tasks, and manage context switches, which consumes a lot of mental effort. Go provides a mechanism where programmers only define tasks and let the system schedule them on CPUs.

Goroutines in Go are similar to threads but are managed by the Go runtime, which intelligently distributes goroutine work across CPUs. This built‑in scheduling and context‑switching makes Go a modern language for concurrent applications.

Using Goroutine

In Go, you start a goroutine by prefixing a function call with the go keyword. Each goroutine corresponds to a single function, and you can launch many goroutines to run the same function.

Starting a Single Goroutine

Example:

func hello() {
    fmt.Println("Hello Goroutine!")
}

func main() {
    hello()
    fmt.Println("main goroutine done!")
}

This runs serially, printing "Hello Goroutine!" followed by "main goroutine done!".

Adding go before the call runs hello concurrently:

func main() {
    go hello() // start another goroutine
    fmt.Println("main goroutine done!")
}

Now only "main goroutine done!" appears because the program exits when main returns, terminating all other goroutines. To keep the program alive, a simple time.Sleep can be used.

func main() {
    go hello()
    fmt.Println("main goroutine done!")
    time.Sleep(time.Second)
}

This prints the main message first, then the goroutine message after the sleep.

Starting Multiple Goroutines

You can launch many goroutines and synchronize them with sync.WaitGroup:

var wg sync.WaitGroup

func hello(i int) {
    defer wg.Done()
    fmt.Println("Hello Goroutine!", i)
}

func main() {
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go hello(i)
    }
    wg.Wait()
}

Each run prints numbers in a nondeterministic order because the goroutines execute concurrently.

Note

If the main goroutine exits, all other goroutines are terminated.

Goroutine vs. Thread

Growable Stack

OS threads typically have a fixed stack (e.g., 2 MB). A goroutine starts with a tiny stack (about 2 KB) that can grow and shrink on demand, up to 1 GB, allowing creation of hundreds of thousands of goroutines.

Goroutine Scheduling

The Go runtime uses a G‑P‑M model:

G represents a goroutine, storing its execution context.

P manages a set of runnable G’s and holds processor‑local resources.

M is a kernel thread that executes G’s assigned by a P.

P’s count is set by runtime.GOMAXPROCS (default equals the number of physical CPUs). The scheduler performs m:n scheduling, mapping many lightweight goroutines onto a smaller set of OS threads, reducing kernel‑mode switches and improving performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoGoroutineWaitGroupParallel Programmingruntime scheduling
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.