Goroutines
Context
Section titled “Context”A goroutine is a lightweight thread managed by the Go runtime. You start a goroutine by prefixing a function call with the keyword go. Goroutines are cheap (kilobytes of stack) and you can run thousands concurrently.
Example
Section titled “Example”Start a goroutine that prints a message while the main function continues.
Code example
Section titled “Code example”package main
import ( "fmt" "time")
func sayHello() { fmt.Println("Hello from goroutine")}
func main() { go sayHello() time.Sleep(100 * time.Millisecond) // give goroutine time to run fmt.Println("Hello from main")}Output
Section titled “Output”Hello from goroutineHello from main