Variables
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. By default, V does not allow global variables, but you can enable them for low-level code. See Global variables for more 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
By default, all variables are immutable.
To be able to change the value of the variable, declare it with mut
.
To change the value of the variable you can use =
.
Try to run 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.
This code will not compile, because the variable age
is not declared.
All variables need to be declared in V.
The values of multiple variables can be changed in one line. In this way, their values can be swapped without an intermediary variable.
Variable shadowing
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.
Unused variables
By default, in development mode, V will only warn you if you have unused variables.
In production mode it will not compile at all (like in Go):