The coroutine

uses the go keyword followed by a function call, you can put the execution process of this function in another coroutine (newbies can be roughly understood as new processes)

go func () ( 
sleep ( 3); // wait for 3 seconds 
print ("Inside!"); 
} () 
Print ("Outside") 

multi-coroutine concurrent task

If you have a series of tasks, and the parameters of the task are placed an inner array, the array asyncMap method can help you perform these tasks concurrently

var Start = now (); 
var RES = [. 1, 2,. 3,. 4] .asyncMap (E => { 
SLEEP ( 3); // If calculating e * e is a behavior that takes 3 seconds 
return e * e; 
}, 4); // 4 concurrent 
print (fmt.Sprintf ("Cost time% v, Result:% v ", now (). Sub (start), res)); 
  • Substitute the .asyncMap of the above example to test the situation without concurrency *

Concurrent Lock

Concurrent map

Refer to map, such as

// concurrent security map
m = new(sync.Map) 

block of code

by the following method can guarantee global concurrent scenarios to count only the content coroutine execution code, count default. 1

synchronized (lock, [count]) {
    // code content

where the lock parameter can be string type (recommended) or other serializable objects