Sobes.tech
Senior

How does Golang work — concurrently, parallel, or can it do both?

sobes.tech AI

Answer from AI

Golang supports concurrency, but not directly parallelism. Concurrency is the ability to efficiently manage multiple tasks by switching between them, while parallelism is the execution of multiple tasks simultaneously.

In Go, concurrency is implemented through goroutines — lightweight threads scheduled by the Go runtime. They can run in parallel if there are multiple CPU cores, thanks to the built-in scheduler.

Thus, Go can operate both concurrently and in parallel, depending on hardware resources and environment settings.

Example of creating a goroutine:

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

func main() {
    go sayHello() // runs the function concurrently
    time.Sleep(time.Second) // wait to allow the goroutine to complete
}