LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • Naming Rules
  • Valid Names
  • Invalid Names

Was this helpful?

  1. en
  2. examples
  3. section_1

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.

age := 23               // int
name := 'Alice'         // string
is_adult := age > 21    // bool

println(age_str)        // 23
println(name)           // Alice
println(is_adult)       // true

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

mut age := 20       // declare the mutable variable age and assign it to the value 20.
println(age)        // 20
age = 21            // assign a new value to age
println(age)        // 21

Leaving out the mut keyword here would result in an error because the value of an immutable variable cannot be changed.

fn main() {
    age = 20
    println(age)
}

The code above would result in an error during compilation because the variable age is not declared,

fn main() {
    mut age := 20       // we declare the mutable variable age and assign it to the value 20.
    println(age)        // 20
    age := 21           // ERROR
}

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.

_ := "I don't need this value"
println(_) // ERROR: Cannot use `_` as value

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

Valid Names

boby
john_dads
myfamily_number

Invalid Names

IamNotValid
new Make
PreviousKeywordsNextFormatted Print

Last updated 5 years ago

Was this helpful?

These rules are from . V uses Snake Case and prefers it because it is more easy to read, write and understand.

Snake_Case