In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the struct operation in golang". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the struct operation in golang".
Struct is an important technology for implementing object-oriented, which is basically used in conjunction with type declaration type name underlying-type.
Struct is a value type, so its zero value is the zero value of all members. Value types are often used with pointers because of their limitations as function parameters.
Declare type Employee struct {ID int Name string Address string}
One member per line, with 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 one line, for example
Type Employee struct {ID int Name, Address string}
You cannot define a member of a structure with the same name, but you can define a member of a pointer type of a structure with the same name, such as
Type Employee struct {ID int Name, Address string Leader * Employee} initialization
It can be initialized directly at the time of declaration, or it can be declared and assigned one by one. Let's look at the most direct way.
Var empl Employeeempl.ID = 1empl.Name = "foo" empl.Address = "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 struct one by one, no more or less, and can be assigned on the type.
The above code is a little fragile because struct members may be adjusted, so let's improve it and initialize it by member name.
Empl3: = & Employee {ID: 3, Name: "foo", Address: "beijing",}
At this point, the order does not matter, and it does not require me to be complete, and members who are not directly assigned continue to retain zero values.
Pointer-related operations
Because struct is a value type, what is received in the function body is a copy if it is passed as an argument, so it is usually passed with a structure pointer as an argument to the function.
EmplPtr: = & emplemplPtr.Name = "bar" / / equivalent to (* emplPtr) .Name = "bar"
When using variables of struct pointer type, you can omit *, which looks like struct is a reference type, but it is actually a structure 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 the structure is comparable.
The comparison algorithm is that if the values of each member are equal, the two structural variables are equal, otherwise they are not equal.
If the structure type is comparable, it means that it can be used as the key type of map.
Structure nesting and anonymous members
This is a magical mechanism. When you declare an anonymous structure in a structure, you can omit the name of the anonymous structure when you use the members of the anonymous structure. just as the current structure owns the members of the anonymous structure.
The following structure, EmployeeManager, takes the above structure Employee 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 seems to be in order, there is nothing magical about it. Let's take a look at how to use this structure.
Fmt.Println (manager.ManagerLevel) fmt.Println (manager.Name) / / this line fmt.Println (manager.Employee.Name) / / is equivalent to this line
This is not only a simplification, but more importantly, I can see the taste of inheritance, but technically there is no inheritance, but a combination, which not only enjoys the benefits of inheritance, but also avoids the trouble of inheritance.
When using an anonymous structure, you can use its methods directly in addition to its properties.
Thank you for reading, the above is the content of "how to understand the struct operation in golang". After the study of this article, I believe you have a deeper understanding of how to understand the struct operation in golang, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.