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_3

構造体

Previous配列Nextsection_1

Last updated 5 years ago

Was this helpful?

構造体(struct)は、さまざまなデータ型(レコード)を組み合わせて宣言します。構造体は、さまざまな変数のリストをメモリの1つのブロックの中でひとつの名前で物理的にグループ化することで、単一のポインタでさまざまな変数にアクセスしたり、同じアドレスを返す名前で宣言された構造体にアクセスしたりできます。

方面から来た方であればclassと見なすこともできなくはありませんが、構造体はより制約が多くなっています。

struct User {
    name string
    email string
    country string
}

fn main() {
    user := User {
        name: "V developers"
        email: "developers@vlang.io"
        country: "Canada"
    }

    println(user.country)
}

注意: 構造体は(ヒープではなく)スタックに配置されます。

構造体のインスタンスを新たに作成するときには、各フィールドをカンマ,で区切ることもできます。これはワンライナーでインスタンスを作成するときに便利です。

user := User { name: "V developers", email: "developers@vlang.io", country: "Canada" }

&プレフィックス

構造体はヒープに配置して参照することもできます。これを行うには、以下のように&プレフィックスを追加します。

user := &User{"V developers", "developers@vlang.io", "Canada"}
println(user.name)

変数userの型は&Userになり、Userへの参照となります。

アクセス指定子

構造体のフィールドはデフォルトで「private」(モジュールの外からアクセスできない)かつ「イミュータブル」です。これはpubやmutアクセス指定子で変更できます。

struct User {
    email string   // privateかつイミュータブル(デフォルト)
}

mut:を指定すると「privateかつミュータブル」にできます。

struct User {
    email string
mut:
    first_name string  // privateかつミュータブル
    last_name string   // (1つのアクセス指定子で複数のフィールドをまとめて指定できる)
}

pub:を指定すると「publicかつイミュータブル」(リードオンリー)にできます。

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int     // publicかつイミュータブル(リードオンリー)
}

pub mut:を指定すると「public」かつ「親モジュールの中でのみミュータブル」になります。

struct User {
   email string
mut:
   first_name string
   last_name string
pub:
   sin_number int
pub mut:
   phone int    // publicだが親モジュールでのみミュータブル
}

__global:を指定すると、親モジュールの中か外かを問わず「publicかつミュータブル」になります。

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int
pub mut:
    phone int
__global:
    address_1 string    // 親モジュールの中でも外でもpublicかつミュータブル
    address_2 string    // (利用を勧めたくないので'__'で始めている)
    city string
    country string
    zip     string
}

命名のルール

  • structの名前は常に大文字で始めること。

訳注: フィールドのスネークケース縛りは近々変更される可能性あり。

演習

  1. Userの情報を保存して表示する構造体を作成しましょう。

  2. xフィールドとyフィールドを持つPoint構造体を作成し、フィールドをそれぞれprivateとpublicにしましょう。

構造体の中の変数(フィールド)についてはにする。

オブジェクト指向プログラミング
Snake_Case