Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Goroutine

A goroutine is a lightweight thread managed by the Go runtime.

go f(x)

Example: with anonymous function

func main() {
	go func() {
		time.Sleep(1 * time.Second) // heavy process
		fmt.Println("goroutine completed")
	}()
	fmt.Println("next")
	time.Sleep(2 * time.Second)
}

Example: named function

func main() {
	go process()
	fmt.Println("next")
	time.Sleep(2 * time.Second)
}

func process() {
	time.Sleep(1 * time.Second) // heavy process
	fmt.Println("goroutine completed")
}

Practice

  1. Goroutines