github.com/aykevl/tinygo@v0.5.0/testdata/coroutines.go (about)

     1  package main
     2  
     3  import "time"
     4  
     5  func main() {
     6  	println("main 1")
     7  	go sub()
     8  	time.Sleep(1 * time.Millisecond)
     9  	println("main 2")
    10  	time.Sleep(2 * time.Millisecond)
    11  	println("main 3")
    12  
    13  	// Await a blocking call. This must create a new coroutine.
    14  	println("wait:")
    15  	wait()
    16  	println("end waiting")
    17  
    18  	// Run a non-blocking call in a goroutine. This should be turned into a
    19  	// regular call, so should be equivalent to calling nowait() without 'go'
    20  	// prefix.
    21  	go nowait()
    22  	time.Sleep(time.Millisecond)
    23  	println("done with non-blocking goroutine")
    24  }
    25  
    26  func sub() {
    27  	println("sub 1")
    28  	time.Sleep(2 * time.Millisecond)
    29  	println("sub 2")
    30  }
    31  
    32  func wait() {
    33  	println("  wait start")
    34  	time.Sleep(time.Millisecond)
    35  	println("  wait end")
    36  }
    37  
    38  func nowait() {
    39  	println("non-blocking goroutine")
    40  }