package main
import (
"fmt"
"time"
)
func print(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
// Synchronous function call
print("synchronous call")
// Asynchronous function call
// To run this function in a goroutine, use go f(s)
// This new goroutine will execute concurrently with the calling main goroutine
go print("asynchronous call")
// Goroutine can also be execute as an anonymous function
go func(msg string) {
fmt.Println(msg)
}("asynchronous anonymous call")
// Two previous function calls are now running asynchronously in separate goroutines
// Wait for them to finish
time.Sleep(time.Second)
fmt.Println("done")
}
// $ go run main.go
// synchronous call : 0
// synchronous call : 1
// synchronous call : 2
// asynchronous anonymous call
// asynchronous call : 0
// asynchronous call : 1
// asynchronous call : 2
// done
Here is an example of goroutines in Golang.