Defined:
is an aggregated data type that is an entity that is aggregated by 0 or more values of any type.
Members:
Each value is called a member of the struct.
Example:
Use the structure of the classic case of the company's employee information, each employee information contains a unique employee number, employee name, home address, date of birth, job, salary, superior leadership and so on. All of this information needs to be bound to an entity, which can be copied as a whole unit, as a function parameter or return value, or stored in an array, and so on.
Define the structure body:
struct { ID int Name string Address String DoB time . Time Position string Salary int int }
Define variables:
var Dilbert Employee
Access Members:
5000
Fetch member Address:
Position: = &Dilbert. Position"" + *position
Point operators and pointers to struct bodies:
var employeeofthemonth *employee = &" (Proactive team player)"
Member Definition Order:
Usually a row corresponds to a struct member whose name is in the previous type, but if the adjacent member type is the same, it can be merged into a single row, as in the following name and address member
struct { ID intstring DoB time . Time Position string Salary int ManagerID int }
Member naming rules:
If the struct member name starts with an uppercase letter, then the member is exported, which is determined by the Go language export rule. A struct may contain both exported and non-exported members.
Export meaning: Can be read and written in other packages.
A struct type named S will no longer contain members of type S: because an aggregated value cannot contain itself. (The restriction also adapts to arrays.) However, the struct of type S can contain *S members of the pointer type, which allows us to create recursive data structures such as linked lists and tree structures.
struct { value int* Tree}
struct face value:
You can specify a value for each member.
struct int }p:= point{12}
The above is the first notation, which requires that each struct member be given a face value in the order defined by the struct member. It's OK:
ANIM: = gif. Gif{loopcount:nframes}
Initialized with a member's name and corresponding value, which can contain some or all of the members, and in this form, if the member is ignored, the default value is 0. Because, given the name of the member, the order in which all members appear is not important.
Note: Two different forms of writing cannot be used in combination.
struct embedding and anonymous members:
struct { intstruct { Center point int struct { Circle circle int}
To access each member:
var8 8 5
In the secondary simplified structure definition:
struct {point int struct {Circleint }
Proud of the anonymous embedding feature, we can directly access the leaf properties without having to give the full path:
var8 // equivalent to W.circle.point.x = 88 // equivalent to W.circle.point.y = 8 5 // equivalent to W.circle.radius = 5 -
struct literals must conform to the structure of the shape type declaration, so you can use only the following two syntaxes, which are equivalent to each other:
w = wheel{circle{point{885= wheel{ circle:circle{ Point: 88}, 5, }, // note:trailing comma necessary here (and at Radius)}
GO language Structure