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
  • The & prefix
  • Access modifiers
  • Exercises

Was this helpful?

  1. en
  2. examples
  3. section_3

Struct

PreviousArraysNextsection_1

Last updated 3 years ago

Was this helpful?

A struct is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing different variables to be accessed via a single pointer or by the struct declared name which returns the same address.

For people coming from languages, it can be thought as class but with more restrictions.

struct User {
    name string
    email string
    country string
}

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

    println(user.country)
}

Note: Structs are allocated on the stack.

You can use a comma to separate each field when creating a new instance of the struct. It's useful when you want to create a new instance on a single line.

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

The & prefix

You can allocate a struct on the heap and get a reference to it by using the & prefix as follows:

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

The type of user is &User. It's a reference to User.

Access modifiers

Struct fields are private and immutable by default. Their access modifiers can be changed with pub and mut.

struct User {
    email string
}

You can define them as private mutable.

struct User {
    email string
mut:
    first_name string
    last_name string
}

You can also define them as public immmutable (readonly).

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int
}

or as public, but mutable only in the parent module.

struct User {
   email string
mut:
   first_name string
   last_name string
pub:
   sin_number int
pub mut:
   phone int
}

or public and mutable both inside and outside parent module.

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int
pub mut:
    phone int
pub mut mut:
    address_1 string
    address_2 string
    city string
    country string
    zip     string
}

Exercises

  1. Create a struct that stores and displays User information.

  2. Create a Point struct that holds x and y field and guard them with private and public.

OOP