struct is an important technology for implementing object-oriented, basically all of which are related to type declarationstype name underlying-type
Use in combination.
struct is a value type, so its zero value is the zero value of all members. Due to the limitations of value types as function parameters, they are often used with pointers.
statement
type Employee struct { ID int Name string Address string }
One member per line, no comma or semicolon in the middle, and uppercase members can be accessed outside the package.
If the type is the same, you can also consider defining it on a line, for example
type Employee struct { ID int Name, Address string }
Members of the same name cannot be defined in a structure, but members of the pointer type of the same name can be defined, for example
type Employee struct { ID int Name, Address string Leader *Employee }
initialization
It can be initialized directly during declaration, or it can be assigned one by one after declaration. Let’s look at the most direct way first.
var empl Employee = 1 = "foo" = "nanshan"
It can also be initialized faster
empl2 := Employee{2, "foo", "nanshan"}
Therefore, the order in which members are declared is very important. The values initialized above must correspond to the members of the struct one by one, no more or less, and can be assigned values in type.
Since the struct member may be adjusted, the above code appears a bit fragile. Here is a little improvement and initialize it according to the member name.
empl3 := &Employee{ ID: 3, Name: "foo", Address: "beijing", }
At this time, the order is no longer important, and I am not required to be complete. Members who have not directly assigned value will continue to retain zero values.
Pointer-related operations
Since struct is a value type, if passed as a parameter, a copy is received in the function body, so when used as a parameter of the function, it is generally passed using a structure pointer.
emplPtr := &empl = "bar" // Equivalent to (*emplPtr).Name = "bar"
When using variables of struct pointer type, you can omit it*
, it looks like struct is a reference type, but it is actually a struct pointer.
The following function initializes a struct and returns its pointer
func EmployeeById(id int) *Employee { return &Employee{ ID: id, Name: "foo", Address: "beijing", } }
Comparability of struct
If each member of the struct is comparable, then this structure is comparable.
The comparison algorithm is: if the values of each member are equal, the two structure variables are equal, otherwise they are not equal.
If the structure type is comparable, it means it can be used as the map's key type.
Structure nesting and anonymous members
This is a magical mechanism. When an anonymous structure is declared in a structure, when a member of the anonymous structure is used, the name of the anonymous structure can be omitted, just as the current structure has members of this anonymous structure.
The structure below,EmployeeManager
, put the above structureEmployee
As an anonymous member
type EmployeeManager struct { Employee // Anonymous member ManagerLevel int }
Initialize anonymous members:
var manager = EmployeeManager{ Employee: Employee{ ID: 2, Name: "fooManager", Address: "beijing", }, ManagerLevel: 4, }
It looks pretty, nothing magical. Let's take a look at how to use this structure
() () //This line() //Equivalent to this line
This kind of struggle is not only a simplification, but more importantly, I see the taste of inheritance, but from a technical perspective, there is no inheritance, but a combination. It enjoys the benefits of inheritance and avoids the trouble of inheritance.
When using an anonymous structure, in addition to directly using its properties, you can also use its methods directly.
This is all about this article about struct in golang. For more related struct content in golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!