The V Playground


Donate via PayPal

Your code will be formatted automatically

fn main() { types := ['game', 'web', 'tools', 'GUI'] for typ in types { println('Hello, $typ developers!') } } fn fib(n int) int { if n <= 1 { return n } return fib(n - 1) + fib(n - 2) } fn main() { for i := 0; i < 10; i++ { println(fib(i)) } } const ( MAX = 15 ) struct Fib { nums []int } fn (fib mut Fib) calc(n int) int { if n <= 1 { return n } if fib.nums[n] != 0 { return fib.nums[n] } fib.nums[n] = fib.calc(n - 1) + fib.calc(n - 2) return fib.nums[n] } fn main() { fib := Fib { nums: [0; MAX] } for i := 0; i < MAX; i++ { println(fib.calc(i)) } } struct User { name string age int is_registered bool } fn main() { s := '[{ "name":"Frodo", "age":25}, {"name":"Bobby", "age":10}]' users := json.decode( []User, s) or { eprintln('Failed to parse json') return } for user in users { println('$user.name: $user.age') } println('') for i, user in users { println('$i) $user.name') if !user.can_register() { println('Cannot register $user.name, they are too young') } } // Let's encode users again just for fun println('') println(json.encode(users)) } fn (u User) can_register() bool { return u.age >= 16 } fn (u mut User) register() { u.is_registered = true } struct User { id int } struct Repo { users []User } fn new_repo() Repo { user := User{id:10} return Repo { users: [user] } } fn (r Repo) find_user_by_id(id int) User? { for user in r.users { if user.id == id { // V automatically wraps this into an option type return user } } return error('User $id not found') } fn main() { repo := new_repo() // Option types must be handled by `or` blocks user := repo.find_user_by_id(10) or { // `or` block must end with `return`, `break`, or `continue` return } println(user.id) // ==> '10' } struct Color { r int g int b int } fn (c Color) str() string { return '{$c.r, $c.g, $c.b}' } fn rgb(r, g, b int) Color { return Color{r: r, g: g, b: b} } const ( NUMBERS = [1, 2, 3] RED = Color{r: 255, g: 0, b: 0} BLUE = rgb(0, 0, 255) ) fn main() { println(NUMBERS) println(RED) println(BLUE) }
 Thank you for trying out V. This is a pre-alpha stage. The language is going to become a lot more stable in the next month. Please report all issues via GitHub. For security reasons imports are temporarily disabled, the size of the program is limited,
and your program can't run for more than 1 second. Coming soon: online C to V translator. Coming not so soon: online C++ to V translator.