mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: add a simple coroutine example
This commit is contained in:
37
examples/coroutines/simple_coroutines.v
Normal file
37
examples/coroutines/simple_coroutines.v
Normal file
@ -0,0 +1,37 @@
|
||||
// Build with
|
||||
// v -use-coroutines simple_coroutines.v
|
||||
//
|
||||
import coroutines
|
||||
import time
|
||||
|
||||
fn foo(a int) {
|
||||
for {
|
||||
println('hello from foo() a=${a}')
|
||||
coroutines.sleep(1 * time.second)
|
||||
}
|
||||
}
|
||||
|
||||
fn foo2(a int) {
|
||||
for {
|
||||
println('hello from foo2() a=${a}')
|
||||
coroutines.sleep(2 * time.second)
|
||||
}
|
||||
}
|
||||
|
||||
fn foo3(a int) {
|
||||
for {
|
||||
println('hello from foo3() a=${a}')
|
||||
coroutines.sleep(3 * time.second)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
go foo(10)
|
||||
go foo2(20)
|
||||
go foo3(30)
|
||||
for {
|
||||
println('hello from MAIN')
|
||||
coroutines.sleep(1 * time.second)
|
||||
}
|
||||
println('done')
|
||||
}
|
Reference in New Issue
Block a user