Junior — Middle
Selgitage, mis on gorutina ja kuidas seda programmeerimises kasutatakse
sobes.tech AI
Vastus AI-lt
A goroutine is a lightweight execution unit similar to a thread in the Go language. It allows functions to run in parallel, created with minimal overhead compared to system threads.
Goroutines are managed by the Go runtime, which efficiently distributes their execution across system threads. This simplifies writing concurrent code and increases performance.
Example of using a goroutine:
package main
import (
"fmt"
"time"
)
func sayHello() {
fmt.Println("Hello from goroutine")
}
func main() {
go sayHello() // start goroutine
time.Sleep(time.Second) // give time for goroutine to execute
}
In this example, the sayHello function is run as a goroutine using the go keyword. The main function waits a second to allow the goroutine to complete its output.