Sobes.tech
Junior — Middle

Explain the concept of concurrency in the Go programming language and its role in developing parallelized applications.

sobes.tech AI

Answer from AI

In Go, concurrency is the ability of a program to perform multiple tasks simultaneously, efficiently utilizing CPU resources. Go provides goroutines for this purpose — lightweight threads that are launched using the go keyword.

Concurrency allows writing parallelized applications where different parts of the code execute simultaneously, increasing performance and responsiveness.

Channels are used for synchronization and data exchange between goroutines, providing safe communication without explicit locking.

Example:

package main
import (
    "fmt"
    "time"
)

func worker(id int) {
    fmt.Printf("Worker %d starting\n", id)
    time.Sleep(time.Second)
    fmt.Printf("Worker %d done\n", id)
}

func main() {
    for i := 1; i <= 3; i++ {
        go worker(i) // start goroutine
    }
    time.Sleep(2 * time.Second) // wait for goroutines to finish
}

In this example, three goroutines run concurrently, demonstrating concurrency in Go.

Explain the concept of concurrency in the Go… - sobes.tech