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!

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

ChooseGolangforhighperformanceandconcurrency,idealforbackendservicesandnetworkprogramming;selectPythonforrapiddevelopment,datascience,andmachinelearningduetoitsversatilityandextensivelibraries.

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

In what aspects are Golang and Python easier to use and have a smoother learning curve? Golang is more suitable for high concurrency and high performance needs, and the learning curve is relatively gentle for developers with C language background. Python is more suitable for data science and rapid prototyping, and the learning curve is very smooth for beginners.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools