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
  • 命名のルール
  • 正しい名前
  • 正しくない名前

Was this helpful?

  1. コード例で学ぶV言語
  2. examples
  3. section_1

変数

Vの変数は:=演算子で宣言および初期化できます。Vの変数はこれ以外の方法では宣言できません。つまり、いかなる変数にも初期値が存在するということです。変数の型は、右辺の値から推測されます。Vの変数はデフォルトでイミュータブル(immutable: 値を改変できない)です。

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

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

メモ: 変数の定義は、関数の内側でしか行なえません。Vにはグローバル変数もグローバルステートも存在しません。

変数の値を変更するには、変数がミュータブル(mutable: 改変可能)になっている必要があります。変数の宣言時にmutキーワードを用いることで、変数をミュータブルにできます。変数に新しい値を代入するには=を用います。

mut age := 20       // ミュータブルな変数ageを宣言して値20を代入する
println(age)        // 20
age = 21            // ageに新しい値を代入する
println(age)        // 21

上のコードはmutキーワードがないとエラーになります(イミュータブルな変数の値は変更できない)。 v

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

上のコードはコンパイルの段階でエラーになります(変数ageが宣言されていないため)。

fn main() {
    mut age := 20       // ミュータブルなage変数を宣言して値20を代入
    println(age)        // 20
    age := 21           // ERROR
}

上のage := 21では、別のエラーがコンパイル時に発生します(変数ageが同じスコープ内で既に定義されているため)。非常にシンプルで覚えやすいルールです。値の宣言は:=で、以後の代入は=と覚えておきましょう。

Goと同様、不要な値は_で受け止めて無視できます。これは値を複数返す関数で使われるのが普通です【TBD】。

_, a := foo()
println(_) // ERROR: Cannot use `_` as value

命名のルール

以下は、変数の命名で守るべきルールの一覧です。

  • 大文字を含んではならない(✖AlphaTest)

  • 区切り文字にはアンダースコアを用いる(○hello_world)

  • できるかぎり、意味の明快な名前を付けること

  • 名前に__を含んではならない

  • 名前に(種類を問わず)スペース文字を含んではならない

  • 名前が11文字を超えたら必ず_で区切らなければならない

正しい名前

boby
john_dads
myfamily_number

正しくない名前

IamNotValid
new Make
PreviousキーワードNext書式付き出力

Last updated 5 years ago

Was this helpful?

上のルールはが由来です。Vではsnake_caseスタイルが用いられ、また推奨されます(読みやすく、書きやすく、理解しやすいため)。

snake_case