Skip to content

Latest commit

 

History

History

01_goroutines

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

🌀 Goroutines: An Introduction

📚 What are Goroutines?

In Go, a goroutine is a lightweight thread of execution. Goroutines are a core feature of Go's concurrency model, allowing developers to perform multiple tasks concurrently with minimal overhead.

A goroutine is a function or method that runs concurrently with other goroutines in the same address space. Unlike threads in other programming languages, goroutines are managed by Go’s runtime system, which makes them much lighter and cheaper in terms of memory and system resources.

Key Characteristics of Goroutines:

  • Lightweight: Goroutines are very lightweight compared to traditional threads, and you can run thousands (or even millions) of goroutines simultaneously.
  • Concurrent: Multiple goroutines can run concurrently, making your application highly scalable and performant.
  • Managed by Go runtime: The Go runtime schedules and manages goroutines, so you don’t need to worry about low-level thread management.

🧠 How to Create a Goroutine

A goroutine is created by using the go keyword before calling a function. Here's a basic example:

package main

import "fmt"

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

func main() {
    go sayHello()  // Launches a new goroutine
    fmt.Println("Hello from main!")
}

In the example above:

  • The go sayHello() statement creates a new goroutine that runs the sayHello function concurrently with the rest of the program.
  • The main function continues executing, printing "Hello from main!" immediately, without waiting for the sayHello goroutine to complete.

Important Points:

  • The main function will exit before the goroutine has a chance to run unless we use synchronization techniques (e.g., sync.WaitGroup).
  • Goroutines share the memory space of the program, making them efficient for communication via channels.

⚙️ Goroutines vs Threads

Goroutines are often compared to threads, but there are important differences:

  • Memory Usage: Goroutines are much more memory-efficient than threads. A goroutine typically uses around 2 KB of memory, while a thread might require several megabytes.
  • Scheduling: Go's runtime system handles scheduling and execution of goroutines, while threads are usually scheduled by the operating system.
  • Concurrency: Goroutines are designed to work in a highly concurrent environment, whereas threads often require explicit synchronization mechanisms.

🕹️ When to Use Goroutines

Goroutines are ideal for concurrent tasks such as:

  • Handling multiple I/O operations: For example, when making multiple network requests simultaneously.
  • Processing large amounts of data in parallel: Split tasks into smaller chunks and process them concurrently.
  • Real-time or concurrent systems: Goroutines are perfect for building real-time applications, servers, or any application that requires multitasking.

Best Practices with Goroutines

  • Avoid Goroutine Leaks: Ensure that goroutines terminate correctly by using synchronization tools like sync.WaitGroup or channels to coordinate their execution.
  • Use Channels for Communication: Goroutines can communicate safely with each other using Go channels. This ensures data consistency without locking mechanisms.
  • Be Aware of Race Conditions: Since goroutines share the same memory space, it’s important to avoid race conditions by properly synchronizing access to shared resources (using locks or channels).

🚀 Next Steps

After understanding the basics of goroutines, it's important to learn how to use synchronization techniques such as WaitGroups and Mutexes to handle concurrency more effectively. Check out the next sections in the basics package to deepen your knowledge.