(Ctrl + Enter)

Check out the docs for more examples
fn main() { 
	areas := ['game', 'web', 'tools', 'science', 'systems', 'GUI', 'mobile']
        for area in areas {
                println('Hello, $area 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 {
mut:
        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() {
        mut fib := Fib {
                nums: [0].repeat(MAX)
        }
        for i := 0; i < MAX; i++ {
                println(fib.calc(i))
        }
}
import json

struct User {
	name          string
	age           int
mut: 
	is_registered bool
}

fn main() {
	s := '[{ "name":"Frodo", "age":25}, {"name":"Bobby", "age":10}]'
	mut 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')
			continue 
		}
		users[i].register() // `user` is immutable, we have to modify the array 
	}
	// 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
	name string
}

struct Repo {
	users []User
}

fn new_repo() Repo {
	return Repo {
		users: [User{1, 'Andrew'}, User {2, 'Bob'}, User {10, 'Charles'}]
	}
}

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`
		println('not found')
		return
	}
	println(user.id) // ==> '10'
	println(user.name) // ==> 'Charles'
}
import strings

struct Color {
        r int
        g int
        b int
}

pub 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)
}  
struct Vec {
	x int
	y int
}

pub fn (a Vec) str() string {
	return '{$a.x, $a.y}'
}

fn (a Vec) + (b Vec) Vec {
	return Vec {
		a.x + b.x,
		a.y + b.y
	}
}

fn (a Vec) - (b Vec) Vec {
	return Vec {
		a.x - b.x,
		a.y - b.y
	}
}

fn main() {
	a := Vec{2, 3}
	b := Vec{4, 5}
	println(a + b) 
	println(a - b) 
}
struct User {
	name string
	age  int	 
} 

fn main() { 
	user1 := User{'Bob', 20} 
	user2 := { user1 | name: 'Peter' } 
	println(user1.name)
	println(user1.age)
	println(user2.name) 
	println(user2.age)
} 
 
The playground is running the latest version of V, updated multiple times per
day.

This is an alpha stage, please report all issues via GitHub.