v / vlib / goroutines / examples / basic_goroutine.v
26 lines · 22 sloc · 622 bytes · e21acca549c0df34c98beb389088dbf7cee4c4d0
Raw
1// Example: Basic goroutine usage with `go` keyword.
2//
3// This demonstrates how V's `go` keyword launches goroutines
4// using the GMP scheduler (translated from Go's runtime).
5//
6// `go` launches a lightweight goroutine (like Go's goroutines)
7// `spawn` launches an OS thread (like V's traditional threads)
8module main
9
10import time
11
12fn say(msg string) {
13 for i in 0 .. 5 {
14 println('${msg}: ${i}')
15 time.sleep(100 * time.millisecond)
16 }
17}
18
19fn main() {
20 // Launch goroutines with `go` - lightweight, Go-style concurrency
21 go say('goroutine 1')
22 go say('goroutine 2')
23
24 // Main goroutine continues running
25 say('main')
26}
27