github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/compiler/testdata/goroutine.go (about)

     1  package main
     2  
     3  func regularFunctionGoroutine() {
     4  	go regularFunction(5)
     5  }
     6  
     7  func inlineFunctionGoroutine() {
     8  	go func(x int) {
     9  	}(5)
    10  }
    11  
    12  func closureFunctionGoroutine() {
    13  	n := 3
    14  	go func(x int) {
    15  		n = 7
    16  	}(5)
    17  	print(n) // note: this is racy (but good enough for this test)
    18  }
    19  
    20  func funcGoroutine(fn func(x int)) {
    21  	go fn(5)
    22  }
    23  
    24  func recoverBuiltinGoroutine() {
    25  	// This is a no-op.
    26  	go recover()
    27  }
    28  
    29  func copyBuiltinGoroutine(dst, src []byte) {
    30  	// This is not run in a goroutine. While this copy operation can indeed take
    31  	// some time (if there is a lot of data to copy), there is no race-free way
    32  	// to make use of the result so it's unlikely applications will make use of
    33  	// it. And doing it this way should be just within the Go specification.
    34  	go copy(dst, src)
    35  }
    36  
    37  func closeBuiltinGoroutine(ch chan int) {
    38  	// This builtin is executed directly, not in a goroutine.
    39  	// The observed behavior is the same.
    40  	go close(ch)
    41  }
    42  
    43  func regularFunction(x int)
    44  
    45  type simpleInterface interface {
    46  	Print(string)
    47  }
    48  
    49  func startInterfaceMethod(itf simpleInterface) {
    50  	go itf.Print("test")
    51  }