Skip to content

Goroutines

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.

Start a goroutine that prints a message while the main function continues.

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")
}
Terminal window
Hello from goroutine
Hello from main