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 for details).

    1. mut age := 20
    2. println(age)
    3. 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.

    This code will not compile, because the variable age is not declared. All variables need to be declared in V.

    1. fn main() {
    2. age := 21

    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).

    You can shadow imported modules though, as it is very useful in some situations:

    1. import ui
    2. fn draw(ctx &gg.Context) {
    3. gg := ctx.parent.get_ui().gg
    4. gg.draw_rect(...)
    5. }