Demystifying Concurrency in Go: A Beginner's Guide with?Examples
Luis Soares, M.Sc.
Lead Software Engineer | Blockchain & ZK Protocol Engineer | ?? Rust | C++ | Web3 | Solidity | Golang | Cryptography | Author
Concurrency is an essential concept in modern programming, enabling the efficient execution of multiple tasks simultaneously.?
Go, a statically typed programming language developed by Google, is known for its simple and efficient concurrency model.?
In this article, we’ll provide a beginner-friendly explanation of concurrency in Go, including examples to help you understand and implement concurrent programming in your Go projects.
Concurrency vs Parallelism
Before we dive into Go’s concurrency model, it’s crucial to differentiate between concurrency and parallelism.
Concurrency is the ability of a program to manage multiple tasks at the same time, regardless of whether they’re executing simultaneously.?
Parallelism involves running multiple tasks simultaneously, often on separate processors or cores.
While they share similarities, concurrency and parallelism are not the same.
Concurrency is a broader concept that encompasses parallelism.?
Go focuses on concurrency, with parallelism as a possible outcome, depending on the hardware capabilities.
Goroutines: The Heart of Concurrency in Go
The core of Go’s concurrency model is the goroutine.?
A goroutine is a lightweight thread managed by the Go runtime, allowing multiple tasks to run concurrently.?
They consume less memory compared to traditional threads, enabling a Go program to handle thousands of tasks simultaneously.
To start a new goroutine, use the go keyword followed by a function call:
package main
import (
"fmt"
"time"
)
func printNumbers() {
for i := 1; i <= 5; i++ {
time.Sleep(1 * time.Second)
fmt.Println("Number:", i)
}
}
func main() {
go printNumbers()
for i := 1; i <= 5; i++ {
time.Sleep(1 * time.Second)
fmt.Println("Letter:", string('A'-1+i))
}
}
In this example, we have two functions: printNumbers() and main().?
The printNumbers() function prints numbers from 1 to 5, while the main() function prints letters from A to E. By using the go keyword before the printNumbers() function call, we create a new goroutine that executes concurrently with the main() function, resulting in interleaved number and letter outputs.
Channels: Synchronizing and Communicating between Goroutines
Channels are Go’s mechanism for synchronizing and exchanging data between goroutines.?
领英推荐
They allow you to pass data between goroutines in a type-safe and synchronized manner, preventing race conditions and other concurrency-related issues.
To create a channel, use the make() function with the chan keyword, followed by the data type:
ch := make(chan int)
Here’s an example demonstrating how channels can be used to communicate between goroutines:
package main
import (
"fmt"
"time"
)
func sendToChannel(ch chan int) {
for i := 1; i <= 5; i++ {
time.Sleep(1 * time.Second)
ch <- i
}
close(ch)
}
func main() {
ch := make(chan int)
go sendToChannel(ch)
for num := range ch {
fmt.Println("Received:", num)
}
}
In this example, we create a channel ch of type int. The sendToChannel() function sends numbers from 1 to 5 to the channel.?
The main() function receives these numbers and prints them. The channel ensures that the communication between the goroutines is synchronized.
Conclusion
Remember that goroutines are lightweight threads that allow your program to run multiple tasks concurrently.?
To create a goroutine, simply use the go keyword followed by a function call.?
Keep in mind that goroutines are not the same as parallelism; they enable concurrent execution, while parallelism is a possible outcome depending on hardware capabilities.
Channels are used to synchronize and communicate between goroutines, ensuring that data exchange is safe and efficient. You can create channels with the make() function and the chan keyword, followed by the data type.
As you begin exploring concurrency in Go, these basic concepts will help you develop more efficient and scalable applications.?
Don’t hesitate to dive deeper into Go’s concurrency model by studying additional concepts such as select statements, buffered channels, and advanced synchronization mechanisms like WaitGroups and Mutexes.?
Happy coding!
All the best,
Luis Soares
CTO | Head of Engineering | Cyber Security | Blockchain Engineer | NFT | Web3 | DeFi | Data Scientist