Middle+
What are asynchronous, parallel, and concurrent in the context of Go?
sobes.tech AI
Answer from AI
In the context of Go:
-
Asynchrony — executing operations without blocking the main thread, usually through goroutines and channels. It allows launching tasks that can run independently and without waiting for their completion.
-
Parallelism — the simultaneous execution of multiple tasks on different CPU cores. In Go, this is achieved through the goroutine scheduler, which distributes them across system threads.
-
Concurrency — the ability of a program to manage multiple tasks that can run simultaneously or sequentially, ensuring correct access to shared resources.
Example of a goroutine:
func worker(id int) {
fmt.Printf("Worker %d started\n", id)
// work
}
func main() {
for i := 1; i <= 5; i++ {
go worker(i) // asynchronous launch
}
time.Sleep(time.Second) // wait for completion
}