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.
V is a very simple language. Going through this documentation will take you about an hour, and by the end of it you will have pretty much learned the entire language.
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.
Install from source
The major way to get the latest and greatest V, is to install it from source. It is easy, and it usually takes only a few seconds.
Linux, macOS, FreeBSD, etc:
You need git, and a C compiler like tcc, gcc or clang, and make:
git clone https://github.com/vlang/v
cd v
make
See here for how to install the development tools.
Windows:
You need git, and a C compiler like tcc, gcc, clang or msvc:
git clone https://github.com/vlang/v
cd v
make.bat -tcc
NB: You can also pass one of -gcc, -msvc, -clang to make.bat instead,
if you do prefer to use a different C compiler, but -tcc is small, fast, and
easy to install (V will download a prebuilt binary automatically).
For C compiler downloads and more info, see here.
It is recommended to add this folder to the PATH of your environment variables.
This can be done with the command v.exe symlink.
NB: Some antivirus programs (like Symantec) are paranoid about executables with
1 letter names (like v.exe). One possible workaround in that situation is
copying v.exe to vlang.exe (so that the copy is newer), or whitelisting the
V folder in your antivirus program.
Android
Running V graphical apps on Android is also possible via vab.
V Android dependencies: V, Java JDK >= 8, Android SDK + NDK.
- Install dependencies (see vab)
- Connect your Android device
- Run:
git clone https://github.com/vlang/vab && cd vab && v vab.v
./vab --device auto run /path/to/v/examples/sokol/particles
For more details and troubleshooting, please visit the vab GitHub repository.
Hello World
fn main() {
println('hello world')
}
Save this snippet into a file named hello.v. Now do: v run hello.v.
That is assuming you have symlinked your V with
v symlink, as described here. If you haven't yet, you have to type the path to V manually.
Congratulations - you just wrote and executed your first V program!
You can compile a program without execution with v hello.v.
See v help for all supported commands.
From the example above, you can see that functions are declared with the fn keyword.
The return type is specified after the function name.
In this case main doesn't return anything, so there is no return type.
As in many other languages (such as C, Go, and Rust), main is the entry point of your program.
println is one of the few built-in functions.
It prints the value passed to it to standard output.
fn main() declaration can be skipped in one file programs.
This is useful when writing small programs, "scripts", or just learning the language.
For brevity, fn main() will be skipped in this tutorial.
This means that a "hello world" program in V is as simple as
println('hello world')
Running a project folder with several files
Suppose you have a folder with several .v files in it, where one of them
contains your main() function, and the other files have other helper
functions. They may be organized by topic, but still not yet structured
enough to be their own separate reusable modules, and you want to compile
them all into one program.
In other languages, you would have to use includes or a build system to enumerate all files, compile them separately to object files, then link them into one final executable.
In V however, you can compile and run the whole folder of .v files together,
using just v run .. Passing parameters also works, so you can
do: v run . --yourparam some_other_stuff
The above will first compile your files into a single program (named
after your folder/project), and then it will execute the program with
--yourparam some_other_stuff passed to it as CLI parameters.
Your program can then use the CLI parameters like this:
import os
println(os.args)
NB: after a successful run, V will delete the generated executable.
If you want to keep it, use v -keepc run . instead, or just compile
manually with v . .
NB: any V compiler flags should be passed before the run command.
Everything after the source file/folder, will be passed to the program
as is - it will not be processed by V.
Comments
// This is a single line comment.
/*
This is a multiline comment.
/* It can be nested. */
*/
Variables
name := 'Bob'
age := 20
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)
Variables are declared and initialized with :=. This is the only
way to declare variables in V. This means that variables always have an initial
value.
The variable's type is inferred from the value on the right hand side.
To choose a different type, use type conversion:
the expression T(v) converts the value v to the
type T.
Unlike most other languages, V only allows defining variables in functions. Global (module level) variables are not allowed. There's no global state in V (see Pure functions by default for details).
For consistency across different code bases, all variable and function names
must use the snake_case style, as opposed to type names, which must use PascalCase.
Mutable variables
mut age := 20
println(age)
age = 21
println(age)
To change the value of the variable use =. In V, variables are
immutable by default.
To be able to change the value of the variable, you have to declare it with mut.
Try compiling the program above after removing mut from the first line.
Initialization vs assignment
Note the (important) difference between := and =.
:= is used for declaring and initializing, = is used for assigning.
fn main() {
age = 21
}
This code will not compile, because the variable age is not declared.
All variables need to be declared in V.
fn main() {
age := 21
}
The values of multiple variables can be changed in one line. In this way, their values can be swapped without an intermediary variable.
mut a := 0
mut b := 1
println('$a, $b') // 0, 1
a, b = b, a
println('$a, $b') // 1, 0
Declaration errors
In development mode the compiler will warn you that you haven't used the variable
(you'll get an "unused variable" warning).
In production mode (enabled by passing the -prod flag to v – v -prod foo.v)
it will not compile at all (like in Go).
fn main() {
a := 10
if true {
a := 20 // error: redefinition of `a`
}
// warning: unused variable `a`
}
Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name that is already used in a parent scope will cause a compilation error.
You can shadow imported modules though, as it is very useful in some situations:
import ui
import gg
fn draw(ctx &gg.Context) {
gg := ctx.parent.get_ui().gg
gg.draw_rect(10, 10, 100, 50)
}
Strings
name := 'Bob'
assert name.len == 3 // will print 3
assert name[0] == byte(66) // indexing gives a byte, byte(66) == `B`
assert name[1..3] == 'ob' // slicing gives a string 'ob'
// escape codes
windows_newline := '\r\n' // escape special characters like in C
assert windows_newline.len == 2
// arbitrary bytes can be directly specified using `\x##` notation where `#` is
// a hex digit aardvark_str := '\x61ardvark' assert aardvark_str == 'aardvark'
assert '\xc0'[0] == byte(0xc0)
// or using octal escape `\###` notation where `#` is an octal digit
aardvark_str2 := '\141ardvark'
assert aardvark_str2 == 'aardvark'
// Unicode can be specified directly as `\u####` where # is a hex digit
// and will be converted internally to its UTF-8 representation
star_str := '\u2605' // ★
assert star_str == '★'
assert star_str == '\xe2\x98\x85' // UTF-8 can be specified this way too.
In V, a string is a read-only array of bytes. All Unicode characters are encoded using UTF-8:
s := 'hello 🌎' // emoji takes 4 bytes
assert s.len == 10
arr := s.bytes() // convert `string` to `[]byte`
assert arr.len == 10
s2 := arr.bytestr() // convert `[]byte` to `string`
assert s2 == s
String values are immutable. You cannot mutate elements:
mut s := 'hello 🌎'
s[0] = `H` // not allowed
error: cannot assign to
s[i]since V strings are immutable
Note that indexing a string will produce a byte, not a rune nor another string. Indexes
correspond to bytes in the string, not Unicode code points. If you want to convert the byte to a
string, use the .ascii_str() method on the byte:
country := 'Netherlands'
println(country[0]) // Output: 78
println(country[0].ascii_str()) // Output: N
Both single and double quotes can be used to denote strings. For consistency, vfmt converts double
quotes to single quotes unless the string contains a single quote character.
For raw strings, prepend r. Escape handling is not done for raw strings:
s := r'hello\nworld' // the `\n` will be preserved as two characters
println(s) // "hello\nworld"
Strings can be easily converted to integers:
s := '42'
n := s.int() // 42
// all int literals are supported
assert '0xc3'.int() == 195
assert '0o10'.int() == 8
assert '0b1111_0000_1010'.int() == 3850
assert '-0b1111_0000_1010'.int() == -3850
For more advanced string processing and conversions, refer to the
vlib/strconv module.
String interpolation
Basic interpolation syntax is pretty simple - use $ before a variable name. The variable will be
converted to a string and embedded into the literal:
name := 'Bob'
println('Hello, $name!') // Hello, Bob!
It also works with fields: 'age = $user.age'. If you need more complex expressions, use ${}:
'can register = ${user.age > 13}'.
Format specifiers similar to those in C's printf() are also supported. f, g, x, o, b,
etc. are optional and specify the output format. The compiler takes care of the storage size, so
there is no hd or llu.
To use a format specifier, follow this pattern:
${varname:[flags][width][.precision][type]}
- flags: may be zero or more of the following:
-to left-align output within the field,0to use0as the padding character instead of the defaultspacecharacter. (Note: V does not currently support the use of'or#as format flags, and V supports but doesn't need+to right-align since that's the default.) - width: may be an integer value describing the minimum width of total field to output.
- precision: an integer value preceded by a
.will guarantee that many digits after the decimal point, if the input variable is a float. Ignored if variable is an integer. - type:
fandFspecify the input is a float and should be rendered as such,eandEspecify the input is a float and should be rendered as an exponent (partially broken),gandGspecify the input is a float--the renderer will use floating point notation for small values and exponent notation for large values,dspecifies the input is an integer and should be rendered in base-10 digits,xandXrequire an integer and will render it as hexadecimal digits,orequires an integer and will render it as octal digits,brequires an integer and will render it as binary digits,srequires a string (almost never used).
Note: when a numeric type can render alphabetic characters, such as hex strings or special values
like infinity, the lowercase version of the type forces lowercase alphabetics and the uppercase
version forces uppercase alphabetics.
Also note: in most cases, it's best to leave the format type empty. Floats will be rendered by
default as g, integers will be rendered by default as d, and s is almost always redundant.
There are only three cases where specifying a type is recommended:
- format strings are parsed at compile time, so specifing a type can help detect errors then
- format strings default to using lowercase letters for hex digits and the
ein exponents. Use a uppercase type to force the use of uppercase hex digits and an uppercaseEin exponents. - format strings are the most convenient way to get hex, binary or octal strings from an integer.
See Format Placeholder Specification for more information.
x := 123.4567
println('[${x:.2}]') // round to two decimal places => [123.46]
println('[${x:10}]') // right-align with spaces on the left => [ 123.457]
println('[${int(x):-10}]') // left-align with spaces on the right => [123 ]
println('[${int(x):010}]') // pad with zeros on the left => [0000000123]
println('[${int(x):b}]') // output as binary => [1111011]
println('[${int(x):o}]') // output as octal => [173]
println('[${int(x):X}]') // output as uppercase hex => [7B]
println('[${10.0000:.2}]') // remove insignificant 0s at the end => [10]
println('[${10.0000:.2f}]') // do show the 0s at the end, even though they do not change the number => [10.00]
String operators
name := 'Bob'
bobby := name + 'by' // + is used to concatenate strings
println(bobby) // "Bobby"
mut s := 'hello '
s += 'world' // `+=` is used to append to a string
println(s) // "hello world"
All operators in V must have values of the same type on both sides. You cannot concatenate an integer to a string:
age := 10
println('age = ' + age) // not allowed
error: infix expr: cannot use
int(right expression) asstring
We have to either convert age to a string:
age := 11
println('age = ' + age.str())
or use string interpolation (preferred):
age := 12
println('age = $age')
Runes
A rune represents a single Unicode character and is an alias for u32. To denote them, use `
(backticks) :
rocket := `🚀`
A rune can be converted to a UTF-8 string by using the .str() method.
rocket := `🚀`
assert rocket.str() == '🚀'
A rune can be converted to UTF-8 bytes by using the .bytes() method.
rocket := `🚀`
assert rocket.bytes() == [byte(0xf0), 0x9f, 0x9a, 0x80]
Hex, Unicode, and Octal escape sequences also work in a rune literal:
assert `\x61` == `a`
assert `\141` == `a`
assert `\u0061` == `a`
// multibyte literals work too
assert `\u2605` == `★`
assert `\u2605`.bytes() == [byte(0xe2), 0x98, 0x85]
assert `\xe2\x98\x85`.bytes() == [byte(0xe2), 0x98, 0x85]
assert `\342\230\205`.bytes() == [byte(0xe2), 0x98, 0x85]
Note that rune literals use the same escape syntax as strings, but they can only hold one unicode
character. Therefore, if your code does not specify a single Unicode character, you will receive an
error at compile time.
Also remember that strings are indexed as bytes, not runes, so beware:
rocket_string := '🚀'
assert rocket_string[0] != `🚀`
assert 'aloha!'[0] == `a`
A string can be converted to runes by the .runes() method.
hello := 'Hello World 👋'
hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`, ` `, `👋`]
assert hello_runes.string() == hello
Numbers
a := 123
This will assign the value of 123 to a. By default a will have the
type int.
You can also use hexadecimal, binary or octal notation for integer literals:
a := 0x7B
b := 0b01111011
c := 0o173
All of these will be assigned the same value, 123. They will all have type
int, no matter what notation you used.
V also supports writing numbers with _ as separator:
num := 1_000_000 // same as 1000000
three := 0b0_11 // same as 0b11
float_num := 3_122.55 // same as 3122.55
hexa := 0xF_F // same as 255
oct := 0o17_3 // same as 0o173
If you want a different type of integer, you can use casting:
a := i64(123)
b := byte(42)
c := i16(12345)
Assigning floating point numbers works the same way:
f := 1.0
f1 := f64(3.14)
f2 := f32(3.14)
If you do not specify the type explicitly, by default float literals
will have the type of f64.
Float literals can also be declared as a power of ten:
f0 := 42e1 // 420
f1 := 123e-2 // 1.23
f2 := 456e+2 // 45600
Arrays
An array is a collection of data elements of the same type. An array literal is a
list of expressions surrounded by square brackets. An individual element can be
accessed using an index expression. Indexes start from 0:
mut nums := [1, 2, 3]
println(nums) // `[1, 2, 3]`
println(nums[0]) // `1`
println(nums[1]) // `2`
nums[1] = 5
println(nums) // `[1, 5, 3]`
An element can be appended to the end of an array using the push operator <<.
It can also append an entire array.
mut nums := [1, 2, 3]
nums << 4
println(nums) // "[1, 2, 3, 4]"
// append array
nums << [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"
mut names := ['John']
names << 'Peter'
names << 'Sam'
// names << 10 <-- This will not compile. `names` is an array of strings.
val in array returns true if the array contains val. See in operator.
names := ['John', 'Peter', 'Sam']
println('Alex' in names) // "false"
Array Fields
There are two fields that control the "size" of an array:
len: length - the number of pre-allocated and initialized elements in the arraycap: capacity - the amount of memory space which has been reserved for elements, but not initialized or counted as elements. The array can grow up to this size without being reallocated. Usually, V takes care of this field automatically but there are cases where the user may want to do manual optimizations (see below).
mut nums := [1, 2, 3]
println(nums.len) // "3"
println(nums.cap) // "3" or greater
nums = [] // The array is now empty
println(nums.len) // "0"
data is a field (of type voidptr) with the address of the first
element. This is for low-level unsafe code.
Note that the fields are read-only and can't be modified by the user.
Array Initialization
The type of an array is determined by the first element:
[1, 2, 3]is an array of ints ([]int).['a', 'b']is an array of strings ([]string).
The user can explicitly specify the type for the first element: [byte(16), 32, 64, 128].
V arrays are homogeneous (all elements must have the same type).
This means that code like [1, 'a'] will not compile.
The above syntax is fine for a small number of known elements but for very large or empty arrays there is a second initialization syntax:
mut a := []int{len: 10000, cap: 30000, init: 3}
This creates an array of 10000 int elements that are all initialized with 3. Memory
space is reserved for 30000 elements. The parameters len, cap and init are optional;
len defaults to 0 and init to the default initialization of the element type (0
for numerical type, '' for string, etc). The run time system makes sure that the
capacity is not smaller than len (even if a smaller value is specified explicitly):
arr := []int{len: 5, init: -1}
// `arr == [-1, -1, -1, -1, -1]`, arr.cap == 5
// Declare an empty array:
users := []int{}
Setting the capacity improves performance of pushing elements to the array as reallocations can be avoided:
mut numbers := []int{cap: 1000}
println(numbers.len) // 0
// Now appending elements won't reallocate
for i in 0 .. 1000 {
numbers << i
}
Note: The above code uses a range for statement.
You can initialize the array by accessing the it variable which gives
the index as shown here:
count := []int{len: 4, init: it}
assert count == [0, 1, 2, 3]
mut square := []int{len: 6, init: it * it}
// square == [0, 1, 4, 9, 16, 25]
Array Types
An array can be of these types:
| Types | Example Definition |
| ------------ | ------------------------------------ |
| Number | []int,[]i64 |
| String | []string |
| Rune | []rune |
| Boolean | []bool |
| Array | [][]int |
| Struct | []MyStructName |
| Channel | []chan f64 |
| Function | []MyFunctionType []fn (int) bool |
| Interface | []MyInterfaceName |
| Sum Type | []MySumTypeName |
| Generic Type | []T |
| Map | []map[string]f64 |
| Enum | []MyEnumType |
| Alias | []MyAliasTypeName |
| Thread | []thread int |
| Reference | []&f64 |
| Shared | []shared MyStructType |
Example Code:
This example uses Structs and Sum Types to create an array which can handle different types (e.g. Points, Lines) of data elements.
struct Point {
x int
y int
}
struct Line {
p1 Point
p2 Point
}
type ObjectSumType = Line | Point
mut object_list := []ObjectSumType{}
object_list << Point{1, 1}
object_list << Line{
p1: Point{3, 3}
p2: Point{4, 4}
}
dump(object_list)
/*
object_list: [ObjectSumType(Point{
x: 1
y: 1
}), ObjectSumType(Line{
p1: Point{
x: 3
y: 3
}
p2: Point{
x: 4
y: 4
}
})]
*/
Multidimensional Arrays
Arrays can have more than one dimension.
2d array example:
mut a := [][]int{len: 2, init: []int{len: 3}}
a[0][1] = 2
println(a) // [[0, 2, 0], [0, 0, 0]]
3d array example:
mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}}
a[0][1][1] = 2
println(a) // [[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
Array methods
All arrays can be easily printed with println(arr) and converted to a string
with s := arr.str().
Copying the data from the array is done with .clone():
nums := [1, 2, 3]
nums_copy := nums.clone()
Arrays can be efficiently filtered and mapped with the .filter() and
.map() methods:
nums := [1, 2, 3, 4, 5, 6]
even := nums.filter(it % 2 == 0)
println(even) // [2, 4, 6]
// filter can accept anonymous functions
even_fn := nums.filter(fn (x int) bool {
return x % 2 == 0
})
println(even_fn)
words := ['hello', 'world']
upper := words.map(it.to_upper())
println(upper) // ['HELLO', 'WORLD']
// map can also accept anonymous functions
upper_fn := words.map(fn (w string) string {
return w.to_upper()
})
println(upper_fn) // ['HELLO', 'WORLD']
it is a builtin variable which refers to the element currently being
processed in filter/map methods.
Additionally, .any() and .all() can be used to conveniently test
for elements that satisfy a condition.
nums := [1, 2, 3]
println(nums.any(it == 2)) // true
println(nums.all(it >= 2)) // false
There are further built-in methods for arrays:
a.repeat(n)concatenates the array elementsntimesa.insert(i, val)inserts a new elementvalat indexiand shifts all following elements to the righta.insert(i, [3, 4, 5])inserts several elementsa.prepend(val)inserts a value at the beginning, equivalent toa.insert(0, val)a.prepend(arr)inserts elements of arrayarrat the beginninga.trim(new_len)truncates the length (ifnew_length < a.len, otherwise does nothing)a.clear()empties the array without changingcap(equivalent toa.trim(0))a.delete_many(start, size)removessizeconsecutive elements from indexstart– triggers reallocationa.delete(index)equivalent toa.delete_many(index, 1)a.delete_last()removes the last elementa.first()equivalent toa[0]a.last()equivalent toa[a.len - 1]a.pop()removes the last element and returns ita.reverse()makes a new array with the elements ofain reverse ordera.reverse_in_place()reverses the order of elements inaa.join(joiner)concatenates an array of strings into one string usingjoinerstring as a separator
See also vlib/arrays.
Sorting Arrays
Sorting arrays of all kinds is very simple and intuitive. Special variables a and b
are used when providing a custom sorting condition.
mut numbers := [1, 3, 2]
numbers.sort() // 1, 2, 3
numbers.sort(a > b) // 3, 2, 1
struct User {
age int
name string
}
mut users := [User{21, 'Bob'}, User{20, 'Zarkon'}, User{25, 'Alice'}]
users.sort(a.age < b.age) // sort by User.age int field
users.sort(a.name > b.name) // reverse sort by User.name string field
V also supports custom sorting, through the sort_with_compare array method.
Which expects a comparing function which will define the sort order.
Useful for sorting on multiple fields at the same time by custom sorting rules.
The code below sorts the array ascending on name and descending age.
struct User {
age int
name string
}
mut users := [User{21, 'Bob'}, User{65, 'Bob'}, User{25, 'Alice'}]
custom_sort_fn := fn (a &User, b &User) int {
// return -1 when a comes before b
// return 0, when both are in same order
// return 1 when b comes before a
if a.name == b.name {
if a.age < b.age {
return 1
}
if a.age > b.age {
return -1
}
return 0
}
if a.name < b.name {
return -1
} else if a.name > b.name {
return 1
}
return 0
}
users.sort_with_compare(custom_sort_fn)
Array Slices
A slice is a part of a parent array. Initially it refers to the elements
between two indices separated by a .. operator. The right-side index must
be greater than or equal to the left side index.
If a right-side index is absent, it is assumed to be the array length. If a left-side index is absent, it is assumed to be 0.
nums := [0, 10, 20, 30, 40]
println(nums[1..4]) // [10, 20, 30]
println(nums[..4]) // [0, 10, 20, 30]
println(nums[1..]) // [10, 20, 30, 40]
In V slices are arrays themselves (they are not distinct types). As a result all array operations may be performed on them. E.g. they can be pushed onto an array of the same type:
array_1 := [3, 5, 4, 7, 6]
mut array_2 := [0, 1]
array_2 << array_1[..3]
println(array_2) // `[0, 1, 3, 5, 4]`
A slice is always created with the smallest possible capacity cap == len (see
cap above) no matter what the capacity or length
of the parent array is. As a result it is immediately reallocated and copied to another
memory location when the size increases thus becoming independent from the
parent array (copy on grow). In particular pushing elements to a slice
does not alter the parent:
mut a := [0, 1, 2, 3, 4, 5]
mut b := a[2..4]
b[0] = 7 // `b[0]` is referring to `a[2]`
println(a) // `[0, 1, 7, 3, 4, 5]`
b << 9
// `b` has been reallocated and is now independent from `a`
println(a) // `[0, 1, 7, 3, 4, 5]` - no change
println(b) // `[7, 3, 9]`
Appending to the parent array may or may not make it independent from its child slices. The behaviour depends on the parent's capacity and is predictable:
mut a := []int{len: 5, cap: 6, init: 2}
mut b := a[1..4]
a << 3
// no reallocation - fits in `cap`
b[2] = 13 // `a[3]` is modified
a << 4
// a has been reallocated and is now independent from `b` (`cap` was exceeded)
b[1] = 3 // no change in `a`
println(a) // `[2, 2, 2, 13, 2, 3, 4]`
println(b) // `[2, 3, 13]`
You can call .clone() on the slice, if you do want to have an independent copy right away:
mut a := [0, 1, 2, 3, 4, 5]
mut b := a[2..4].clone()
b[0] = 7 // NB: `b[0]` is NOT referring to `a[2]`, as it would have been, without the .clone()
println(a) // [0, 1, 2, 3, 4, 5]
println(b) // [7, 3]
Slices with negative indexes
V supports array and string slices with negative indexes.
Negative indexing starts from the end of the array towards the start,
for example -3 is equal to array.len - 3.
Negative slices have a different syntax from normal slices, i.e. you need
to add a gate between the array name and the square bracket: a#[..-3].
The gate specifies that this is a different type of slice and remember that
the result is "locked" inside the array.
The returned slice is always a valid array, though it may be empty:
a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
println(a#[-3..]) // [7, 8, 9]
println(a#[-20..]) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
println(a#[-20..-8]) // [0, 1]
println(a#[..-3]) // [0, 1, 2, 3, 4, 5, 6]
// empty arrays
println(a#[-20..-10]) // []
println(a#[20..10]) // []
println(a#[20..30]) // []
Array method chaining
You can chain the calls of array methods like .filter() and .map() and use
the it built-in variable to achieve a classic map/filter functional paradigm:
// using filter, map and negatives array slices
files := ['pippo.jpg', '01.bmp', '_v.txt', 'img_02.jpg', 'img_01.JPG']
filtered := files.filter(it#[-4..].to_lower() == '.jpg').map(it.to_upper())
// ['PIPPO.JPG', 'IMG_02.JPG', 'IMG_01.JPG']
Fixed size arrays
V also supports arrays with fixed size. Unlike ordinary arrays, their length is constant. You cannot append elements to them, nor shrink them. You can only modify their elements in place.
However, access to the elements of fixed size arrays is more efficient, they need less memory than ordinary arrays, and unlike ordinary arrays, their data is on the stack, so you may want to use them as buffers if you do not want additional heap allocations.
Most methods are defined to work on ordinary arrays, not on fixed size arrays. You can convert a fixed size array to an ordinary array with slicing:
mut fnums := [3]int{} // fnums is a fixed size array with 3 elements.
fnums[0] = 1
fnums[1] = 10
fnums[2] = 100
println(fnums) // => [1, 10, 100]
println(typeof(fnums).name) // => [3]int
fnums2 := [1, 10, 100]! // short init syntax that does the same (the syntax will probably change)
anums := fnums[..] // same as `anums := fnums[0..fnums.len]`
println(anums) // => [1, 10, 100]
println(typeof(anums).name) // => []int
Note that slicing will cause the data of the fixed size array to be copied to the newly created ordinary array.
Maps
mut m := map[string]int{} // a map with `string` keys and `int` values
m['one'] = 1
m['two'] = 2
println(m['one']) // "1"
println(m['bad_key']) // "0"
println('bad_key' in m) // Use `in` to detect whether such key exists
println(m.keys()) // ['one', 'two']
m.delete('two')
Maps can have keys of type string, rune, integer, float or voidptr.
The whole map can be initialized using this short syntax:
numbers := {
'one': 1
'two': 2
}
println(numbers)
If a key is not found, a zero value is returned by default:
sm := {
'abc': 'xyz'
}
val := sm['bad_key']
println(val) // ''
intm := {
1: 1234
2: 5678
}
s := intm[3]
println(s) // 0
It's also possible to use an or {} block to handle missing keys:
mm := map[string]int{}
val := mm['bad_key'] or { panic('key not found') }
The same optional check applies to arrays:
arr := [1, 2, 3]
large_index := 999
val := arr[large_index] or { panic('out of bounds') }
println(val)
// you can also do this, if you want to *propagate* the access error:
val2 := arr[333] ?
println(val2)