结构体(Structs)
struct Point {
x int
y int
}
fn main() {
p := Point{
x: 10
y: 20
}
println(p.x) // Struct fields are accessed using a dot
}
``
structs
在堆栈上分配。 要在堆上分配结构并获取指向它的指针,请使用&
前缀:
pointer := &Point{10, 10} // Alternative initialization syntax for structs with 3 fields or fewer
println(pointer.x) // Pointers have the same syntax for accessing fields
``
V
没有子类,但它支持嵌入式结构:
// TODO: this will be implemented later in July
struct Button {
Widget
title string
}
button := new_button('Click me')
button.set_pos(x, y)
// Without embedding we'd have to do
button.widget.set_pos(x,y)