V is a statically typed compiled programming language designed for building maintainable software.
It’s similar to Go and its design has also been influenced by Oberon, Rust, Swift, Kotlin, and Python.
The language promotes writing simple and clear code with minimal abstraction.
Despite being simple, V gives the developer a lot of power. Anything you can do in other languages, you can do in V.
1// Single Line Comment.
2/*
3 Multi Line Comment
4*/
5
6struct User { // Cannot be defined in main, explained later.
7 age int
8 name string
9 pos int = -1 // custom default value
10}
11// struct method
12fn (u User) can_register() bool {
13 return u.age > 16
14}
15
16struct Parser {
17 token Token
18}
19
20// c like enums
21enum Token {
22 plus
23 minus
24 div
25 mult
26}
27
28// 1. functions
29// language does not use semi colons
30fn add(x int, y int) int {
31 return x + y
32}
33// can return multiple values
34fn foo() (int, int) {
35 return 2, 3
36}
37
38// function visibility
39pub fn public_function() { // pub can only be used from a named module.
40}
41
42fn private_function() {
43}
44
45
46
47// Main function
48fn main() {
49 // Anonymous functions can be declared inside other functions:
50 double_fn := fn (n int) int {
51 return n + n
52 }
53 // 2. Variables: they are immutable by default
54 // implicitly typed
55 x := 1
56 // x = 2 // error
57 mut y := 2
58 y = 4
59 name := "John"
60 large_number := i64(9999999999999)
61 println("$x, $y, $name, $large_number") // 1, 4, John, 9999999999999
62
63 // unpacking values from functions.
64 a, b := foo()
65 println("$a, $b") // 2, 3
66 c, _ := foo() // ignore values using `_`
67 println("$c") // 2
68
69 // Numbers
70 u := u16(12)
71 v := 13 + u // v is of type `u16`
72 r := f32(45.6)
73 q := r + 3.14 // x is of type `f32`
74 s := 75 // a is of type `int`
75 l := 14.7 // b is of type `f64`
76 e := u + s // c is of type `int`
77 d := l + r // d is of type `f64`
78
79 // Strings
80 mut bob := 'Bob'
81 assert bob[0] == u8(66) // indexing gives a byte, u8(66) == `B`
82 assert bob[1..3] == 'ob' // slicing gives a string 'ob'
83 bobby := bob + 'by' // + is used to concatenate strings
84 println(bobby) // "Bobby"
85 bob += "by2" // += is used to append to strings
86 println(bob) // "Bobby2"
87
88 //String values are immutable. You cannot mutate elements:
89 //mut s := 'hello 🌎'
90 //s[0] = `H` // not allowed
91
92 //For raw strings, prepend r. Escape handling is not done for raw strings:
93 rstring := r'hello\nworld' // the `\n` will be preserved as two characters
94 println(rstring) // "hello\nworld"
95
96 // string interpolation
97 println('Hello, $bob!') // Hello, Bob!
98 println('Bob length + 10: ${bob.len + 10}!') // Bob length + 10: 13!
99
100 // 3. Arrays
101 mut numbers := [1, 2, 3]
102 println(numbers) // `[1, 2, 3]`
103 numbers << 4 // append elements with <<
104 println(numbers[3]) // `4`
105 numbers[1] = 5
106 println(numbers) // `[1, 5, 3]`
107 // numbers << "John" // error: `numbers` is an array of numbers
108 numbers = [] // array is now empty
109 arr := []int{len: 5, init: -1}
110 // `arr == [-1, -1, -1, -1, -1]`, arr.cap == 5
111
112 number_slices := [0, 10, 20, 30, 40]
113 println(number_slices[1..4]) // [10, 20, 30]
114 println(number_slices[..4]) // [0, 10, 20, 30]
115 println(number_slices[1..]) // [10, 20, 30, 40]
116
117 // 4. structs and enums
118 // struct User {
119 // age int
120 // name string
121 // pos int = -1 // custom default value
122 // }
123 mut users := User{21, 'Bob', 0}
124 println(users.age) // 21
125
126 // enum Token {
127 // plus
128 // minus
129 // div
130 // mult
131 // }
132
133 // struct Parser {
134 // token Token
135 // }
136 parser := Parser{}
137 if parser.token == .plus || parser.token == .minus
138 || parser.token == .div || parser.token == .mult {
139 // ...
140 }
141
142
143 // 5. Maps
144 number_map := {
145 'one': 1
146 'two': 2
147 }
148 println(number_map) // {'one': 1, 'two': 2}
149 println(number_map["one"]) // 1
150 mut m := map[string]int{} // a map with `string` keys and `int` values
151 m['one'] = 1
152 m['two'] = 2
153 println(m['one']) // "1"
154 println(m['bad_key']) // "0"
155 m.delete('two')
156
157 // 6. Conditionals
158 a_number := 10
159 b_number := 20
160 if a_number < b {
161 println('$a_number < $b_number')
162 } else if a_number > b {
163 println('$a_number > $b_number')
164 } else {
165 println('$a_number == $b_number')
166 }
167 num := 777
168 even_odd := if num % 2 == 0 { 'even' } else { 'odd' }
169 println(even_odd)
170
171 match even_odd {
172 'even' { println('even') }
173 'odd' { println('odd') }
174 else { println('unknown') }
175 }
176
177 // 7. Loops
178 loops := [1, 2, 3, 4, 5]
179 for lp in loops {
180 println(lp)
181 }
182 loop_names := ['Sam', 'Peter']
183 for i, lname in loop_names {
184 println('$i) $lname')
185 // Output: 0) Sam
186 // 1) Peter
187 }
188 // You can also use break and continue followed by a
189 // label name to refer to an outer for loop:
190 outer: for i := 4; true; i++ {
191 println(i)
192 for {
193 if i < 7 {
194 continue outer
195 } else {
196 break outer
197 }
198 }
199 }
200}
Further reading ¶
There are more complex concepts to be learnt in V which are available at the official V documentation.
You can also find more information about the V language at the official website or check it out at the v playground.