Interview Question: What are Goroutines?

Publish date: 2025-02-11
Tags: Go, Interview-Questions

Concurrency is a cornerstone of modern software engineering, and Go (Golang) makes it accessible with Goroutines. If you’re preparing for a software engineering interview, understanding Goroutines is crucial. This guide breaks down Goroutines in simple terms, complete with examples and diagrams.

What are Goroutines?

What Are Goroutines?

A Goroutine is a lightweight execution thread managed by the Go runtime. Think of it as a function that runs independently and concurrently with other Goroutines.

Key Features:

The Main Goroutine

Every Go program starts with a main Goroutine. If it exits, all other Goroutines terminate immediately.

Goroutines vs. Threads

Feature Goroutines OS Threads
Memory 2KB 1-2MB
Management Go runtime Operating System
Scheduling M:N model (see below) 1:1 with OS threads

What are the differences between Goroutines and Threads?

Concurrency vs. Parallelism

Goroutines enable concurrency, but parallelism depends on available CPU cores.

The Fork-Join Model

Go uses the fork-join concurrency model:

Example without a Join Point:

func main() {  
    go fmt.Println("Hello") // Fork  
    // Main Goroutine exits before "Hello" prints  
}  

Here, "Hello" might never print because the main Goroutine exits too quickly.

Fixing Race Conditions with sync.WaitGroup

Use sync.WaitGroup to create a join point and ensure Goroutines complete:

var wg sync.WaitGroup  

func main() {  
    wg.Add(1) // Add 1 Goroutine to wait for  
    go sayHello()  
    wg.Wait() // Join point: wait until Done() is called  
}  

func sayHello() {  
    defer wg.Done() // Signal completion  
    fmt.Println("Hello")  
}  

Goroutine Scheduling: Preemptive or Nonpreemptive?

Goroutines are nonpreemptive but appear preemptive because the Go runtime:

  1. Observes blocking operations (e.g., I/O, channel operations).
  2. Automatically suspends/resumes Goroutines.

This hybrid model simplifies concurrency without manual yield points.
Goroutine Scheduling: Preemptive or Nonpreemptive?

Can You Explain the Lifecycle and Scheduling of a Goroutine?

Summary

Practice these concepts to ace concurrency questions in your software engineering interview!

Tags: Go, Interview-Questions