(Most asked Golang concurrency questions and answers)
Deadlock— everyone is waiting, nobody can proceed. Goroutines block forever waiting for each other. No progress is possible.
Livelock— everyone is running, but no progress is made. Goroutines are active (not blocked) but keep stepping out of each other’s way, doing work that prevents progress.
Starvation— one goroutine never gets CPU or data. One goroutine is ready to run, but other goroutines or scheduling policies prevent it from running.
Race Condition— outcome depends on timing. Multiple goroutines access shared data without proper synchronization → final result is nondeterministic.
Blocking— goroutine waits, but may eventually continue. A goroutine is temporarily stuck (waiting for I/O, lock, channel), but will proceed eventually.
A goroutine is a lightweight thread managed by the Go runtime (not the operating system).
Key characteristics of goroutines:
M:N threading model).Preemptive (since Go 1.14+): The runtime can interrupt long-running goroutines, so one goroutine rarely blocks others.package main
import (
“fmt”
”time”
)
func hello() {
fmt.Println(”Hello world”)
}
func main(){
go hello() // launches a new goroutine, using go keyword
time.Sleep(1*time.Second) // main must wait, if not goroutine doesn’t work
}
// Output: Hello world