In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "how to initialize the member variables of the structure in the go language", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to initialize the member variables of the structure in the go language" can help you solve your doubts.
Initialization method: 1, use "ins: = structure type name {field 1: value, field 2: value, …}" statement initialization, suitable for selectively filling the structure with more fields; 2, use the "ins: = structure type name {field 1 value, field 2 value, …}" statement initialization, suitable to fill the structure with fewer fields.
When the structure is instantiated, the member variables can be initialized directly. There are two forms of initialization: the field "key-value pair" form and the list form of multiple values. The initialization of the key-value pair form is suitable for selectively populating the structure with more fields, and the list form of multiple values is suitable for filling the structure with fewer fields.
Initialize the structure using key-value pairs
Structures can initialize fields using key-value pairs (Key value pair). Each Key corresponds to a field in the structure, and the Value of the key corresponds to the value that the field needs to initialize.
The padding of key-value pairs is optional, and fields that do not need to be initialized can not be filled in the initialization list.
The default value of the field after the structure body is instantiated is the default value of the field type, for example, the numeric value is 0, the string is "" (empty string), the Boolean is false, the pointer is nil, and so on.
1) the writing format of the initialization structure of the key-value pair
The initialization format of key-value pairs is as follows:
Ins: = structure type name {Field 1: the value of Field 1, Field 2: the value of Field 2, … }
The following is a description of each part:
Structure type: the type name when defining the structure.
Field 1, field 2: the field name of the structure member, and the field name can only appear once in the field initialization list of the structure type name.
The value of field 1, the value of field 2: the initial value of the structure member field.
The key values are separated by:, and the key value pairs are separated by.
2) examples of filling structures with key-value pairs
The following example describes the character association at home, as sung in the children's song: "Dad's father is a grandfather". Multi-level child can be used to describe and establish relationships between characters. The code to fill the structure with key-value pairs is as follows:
Type People struct {name string child * People} relation: = & People {name: "Grandpa", child: & People {name: "Dad", child: & People {name: "I",}
The code is as follows:
Line 1 defines the People structure.
Line 2, the string field of the structure.
Line 3, the structure pointer field of the structure, type is * People.
In line 6, relation is addressed by the People type, and an instance of type * People is formed.
In line 8, when initializing child, you need a value of type * People to initialize a People with a fetch address.
Tip: structure members can only contain pointer types of structures, including non-pointer types will cause compilation errors.
Initialize the structure with a list of multiple values
The Go language can ignore keys on the basis of key-value pair initialization, that is, you can initialize fields of structures with a list of multiple values.
1) the writing format of the initialization structure of multiple value lists
Multiple values initialize structures separated by commas, for example:
Ins: = structure type name {the value of field 1, the value of field 2, … }
When initializing using this format, you need to be aware of:
All fields of the structure must be initialized.
The filling order of each initial value must be the same as the order in which the fields are declared in the structure.
Key-value pairs cannot be mixed with the initialization form of the values list.
2) examples of initializing structures with multiple value lists
The following example describes an address structure that requires a certain order, such as:
Type Address struct {Province string City string ZipCode int PhoneNumber string} addr: = Address {"Sichuan", "Chengdu", 610000, "0",} fmt.Println (addr)
Run the code and the output is as follows:
{Sichuan Chengdu 610000 0} read here, this article "how to initialize the member variables of a structure in go language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more related articles, welcome to follow the industry information channel.
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.