Home >Backend Development >Golang >How to initialize member variables of a structure in go language
Initialization method: 1. Use the "ins := structure type name {field 1: value, field 2: value,...}" statement to initialize, which is suitable for selectively filling structures with many fields; 2. Use the "ins := structure type name {value of field 1, value of field 2,...}" statement to initialize, which is suitable for structures with fewer fields.
The operating environment of this tutorial: Windows 10 system, GO 1.11.2, Dell G3 computer.
The structure can directly initialize the member variables when instantiating it. There are two forms of initialization: the field "key-value pair" form and the list form of multiple values. Initialization in the form of key-value pairs is suitable. Selectively fill structures with more fields. The list form of multiple values is suitable for structures with fewer fields.
Use "key value pair" to initialize the structure
The structure can use "key value pair" (Key value pair) to initialize the fields. Each "key" (Key) corresponds to a field in the structure, and the "value" (Value) of the key corresponds to the value that the field needs to be initialized.
The filling of key-value pairs is optional, and fields that do not require initialization do not need to be filled in the initialization list.
The default value of the field after the structure is instantiated is the default value of the field type, for example, the value is 0, the string is "" (empty string), the Boolean is false, the pointer is nil, etc.
1) The writing format of the key-value pair initialization structure
The format of the key-value pair initialization is as follows:
ins := 结构体类型名{ 字段1: 字段1的值, 字段2: 字段2的值, … }
The following is a description of each part Description:
Structure type: The type name when defining the structure.
Field 1, Field 2: field names of structure members, field names can only appear once in the field initialization list of structure type names.
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) Example of using key-value pairs to fill a structure
The following example describes the relationship between the characters in the family, just like the children's song: "Dad's dad is "Grandpa", multi-level children can be used to describe and establish relationships between characters. The code for filling the structure in the form of key-value pairs is as follows:
type People struct { name string child *People } relation := &People{ name: "爷爷", child: &People{ name: "爸爸", child: &People{ name: "我", }, }, }
The code description 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, the type is *People.
Line 6, after relation takes the address from the People type, an instance of type *People is formed.
Line 8, when initializing child, it needs a value of type *People, and uses the address to initialize a People.
Tip: Structure members can only contain pointer types of the structure. Containing non-pointer types will cause a compilation error.
Use a list of multiple values to initialize the structure
The Go language can ignore the "key" based on the "key-value pair" initialization , that is, you can initialize the fields of a structure with a list of multiple values.
1) Writing format of multiple value list initialization structure
Use commas to separate multiple values in the initialization structure, for example:
ins := 结构体类型名{ 字段1的值, 字段2的值, … }
Use When initializing this format, please note:
All fields of the structure must be initialized.
The filling order of each initial value must be consistent with the declaration order of the fields in the structure.
The initialization forms of key-value pairs and value lists cannot be mixed.
2) Example of multiple value list initialization structure
The following example describes an address structure, and the addresses are required to be in a certain order. , for example:
type Address struct { Province string City string ZipCode int PhoneNumber string } addr := Address{ "四川", "成都", 610000, "0", } fmt.Println(addr)
Run the code, the output is as follows:
{四川 成都 610000 0}
Recommended learning: Golang tutorial
The above is the detailed content of How to initialize member variables of a structure in go language. For more information, please follow other related articles on the PHP Chinese website!