【Go】Struct

Struct

Declaration Definition

Structure is a set of fields

1
2
3
4
type Vertex struct {
     x int
     y int
}

Accessing Structure Members

Structure fields are accessed via.

1
2
3
v: = Vertex{1, 2}
v.x = 23
v.y = 45

Structure pointer

Structure fields can also be accessed through structure pointers

Structure pointers can be implicitly indirectly referenced, such as

1
2
3
4
v: = Vertex{1, 2}
p: = &v

p.x = 120

Assignment to create a new structure

Use key: value to display some fields that are initialized, and default values for other non-specified fields

1
2
3
4
5
6
7
8
9
10
11
12
13
v1: = Vertex{1, 2}

// now y is 0
v2: = Vertex{x: 1}

// at this time x is 0
v3: = Vertex{y: 1}

// At this point x y is 0
v4: = Vertex()

// v4 is a pointer to vertex
v5: = &Vertex{1, 2}
Persist in original technology sharing, your support will encourage me to continue to create!