Spawning a void function to run concurrently
In this section, we will write a simple function that does not have any return type. The function just prints a message to the console. Then, we will spawn this function so that it runs in a concurrent thread using the go
keyword, as follows:
module main fn greet() { println('Hello from other side!') } fn main() { h := go greet() println(typeof(h).name) // thread }
In the preceding code, the h
variable provides access to handle the concurrent task. The h
variable is of the thread
type in the preceding code. The output of the preceding code could be any of the following outputs:
The following is the first output you may receive:
thread
The following is the second output you may receive:
thread Hello from other side!
You will see either of these outputs, which appear to be inconsistent outcomes from the program. Notice that the greet()
function...