Variables
In V variables can be declared and initialized with the :=
operator. Variables can only be declared this way in V, this means all variables have an initial value. The type of a variable is inferred from the value on the right hand side. By default variables in V are immutable.
Note: Variables can only be defined in a function. There are no global variables and no global state in V.
To change the value of a variable, it needs to be mutable. This can be done using the mut
keyword when declaring the variable. To assign a new value to a variable use =
.
Leaving out the mut
keyword here would result in an error because the value of an immutable variable cannot be changed.
The code above would result in an error during compilation because the variable age
is not declared,
here age := 21
will result in another error while compiling because the variable age
is already defined in the scope. It's very simple to remember, just declare value with :=
and assign value with =
.
Like Go, You can also use _
for ignoring values when it is not needed. Usually used in multi return functions.
Naming Rules
The following are the rules which should be kept in mind while naming variables.
Name should not contain Uppercase letters like
AlphaTest
Use underscores as separators like
hello_world
Name should be descriptive as possible
Name should not contain
__
Name should not contain any space
If the name is longer than 11 then it must use
_
as separator
These rules are from Snake_Case
. V uses Snake Case and prefers it because it is more easy to read, write and understand.
Valid Names
Invalid Names
Last updated