Go Goroutines


Goroutines are a fundamental part of Go’s concurrency model. They’re lightweight, independently executing functions that allow you to write concurrent programs without the complexities of traditional threads. Think of them as functions that can run alongside other functions without blocking each other.

Key Characteristics:

Example:

package main

import (
    "fmt"
    "time"
)

func printNumbers() {
    for i := 1; i <= 5; i++ {
        fmt.Println("Number:", i)
        time.Sleep(500 * time.Millisecond)
    }
}

func printLetters() {
    for char := 'a'; char <= 'e'; char++ {
        fmt.Println("Letter:", string(char))
        time.Sleep(300 * time.Millisecond)
    }
}

func main() {
    // Launch goroutines
    go printNumbers()
    go printLetters()

    // Keep the main goroutine alive long enough for the others to finish.
    time.Sleep(3 * time.Second)
    fmt.Println("Main goroutine finished")
}

Explanation:

  1. printNumbers() and printLetters(): These functions represent tasks we want to run concurrently.
  2. go printNumbers() and go printLetters(): The go keyword launches these functions as goroutines. They will now execute concurrently with the main function.
  3. time.Sleep(): These calls introduce small delays to simulate some work being done. Notice how the output interleaves the numbers and letters, demonstrating concurrent execution.
  4. time.Sleep(3 * time.Second) in main(): This line is crucial. Without it, the main function would likely exit before the other goroutines have a chance to complete their work. In real-world scenarios, you would typically use channels or other synchronization mechanisms to coordinate the execution of goroutines and ensure proper completion.

Conclusion:

Goroutines are a powerful and efficient way to achieve concurrency in Go. Their lightweight nature and ease of use make them a cornerstone of Go’s elegant approach to concurrent programming. Understanding goroutines is essential for writing efficient and scalable Go applications.