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
  • 有效名称
  • 无效名称

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_1

变量

在V语言中,变量可以用:=运算符声明和初始化。变量只能以这种方式在V中声明,这意味着所有变量都有一个初始值。变量的类型是从右边的值推断出来的。默认情况下,V中的变量是不可变的。

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

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

Note:变量只能在函数中定义。所以在V中没有全局变量和全局状态。

要更改变量的值,要确保它必须是可变的。这可以在声明变量时使用mut关键字来完成。要给变量赋值,可以使用=。

mut age := 20       // 声明可变变量age并将其赋给值20。
println(age)        // 20
age = 21            // 为age赋个新的值
println(age)        // 21

在此处省略mut关键字将导致错误,因为无法更改不可变变量的值。

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

上面的代码在编译过程中会导致错误,因为未声明变量age,

fn main() {
    mut age := 20       // 我们声明可变变量age并将其赋给值20。
    println(age)        // 20
    age := 21           // 错误
}

这里的age:=21在编译时将导致另一个错误,因为变量age已在作用域中定义。记住这一点很简单,只要用:=声明值并用=赋值。

像Go一样,您还可以使用_忽略掉不需要的值。通常用于多返回函数。

_ := "I don't need this value"
println(_) // 错误:不能将“_”用作值

Naming Rules

以下是命名变量时应记住的规则。 -名称不应该包含像AlphaTest这样的大写字母-使用下划线作为分隔符,如helloworld` -名称应该尽可能具有描述性 -名称不应该包含__-名称不应该包含任何空格 -如果名称大于11,则必须要使用`作为分隔符

有效名称

boby
john_dads
myfamily_number

无效名称

IamNotValid
new Make
Previous关键字Nextprint系列函数

Last updated 5 years ago

Was this helpful?

这些规则来自 V语言使用Snake Case,并且更喜欢它,因为它更易于阅读、书写和理解。

Snake_Case