| 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) |
| 8 | module main |
| 9 | |
| 10 | import time |
| 11 | |
| 12 | fn say(msg string) { |
| 13 | for i in 0 .. 5 { |
| 14 | println('${msg}: ${i}') |
| 15 | time.sleep(100 * time.millisecond) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn 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 | |