In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use the pointer of Go language". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, define the structure 1. Grammatical format
Structure definition requires type and struct statements
The struct statement defines a new data type with one or more members in the structure
The type statement sets the name of the structure
The format of the structure is as follows
Type struct_variable_type struct {member definition member definition... Member definition}
Once the structure type is defined, it can be used for the declaration of variables. The syntax format is as follows
Variable_name: = structure_variable_type {value1, value2...valuen}
Or
Variable_name: = structure_variable_type {key1: value1, key2: value2..., keyn: valuen} 2. Example package main import "fmt" type Persion struct {name string sex string age int id_card int} func main () {/ / create a new structure fmt.Println (Persion {"zhangsan", "male", 20, 123412424}) / / use the key:value format fmt.Println (Persion {name: "lisi", sex: "female", age: 18 Id_card: 133654623}) / / ignored field is 0 or empty fmt.Println (Persion {name: "wangwu", id_card: 21352365})}
Output result
{zhangsan male 20 123412424}
{lisi female 18 133654623}
{wangwu 0 21352365}
Second, access structure members
If you want to access structure members, you need to use a period. Operator in the format:
A structure. Member name "
Structure type variables are defined using the struct keyword. Examples are as follows:
Package main import "fmt" func main () {/ / struct declares type Persion struct {name string age int sex string id int} / / variable of struct type var (Persion1 Persion / / declares Persion1 as Persion type Persion2 Persion / / declares Persion2 as Persion type) / / assigns prison1 the value Persion1.name = "lisi" Persion1.sex = "man" Persion1.age = 30 Persion1.id = 56341153 / / assign to prison2 Persion2.name = "wangwu" Persion2.sex = "woman" Persion2.age = 18 Persion2.id = 78238232 fmt.Println (Persion1) fmt.Println (Persion2)}
The output is as follows
{lisi 30 man 56341153}
{wangwu 18 woman 78238232}
Third, the structure is used as a function parameter
The structure can also be passed to the function as an argument, which can solve the problem of code redundancy and simplify the code.
Example
Package main import "fmt" / / structure declares type Persion struct {name string age int sex string id int} func main () {/ / variable of structure type var (Persion1 Persion / / declares Persion1 as Persion type Persion2 Persion / / declares Persion2 as Persion type) / / assigns prison1 the value Persion1.name = "lisi" Persion1.sex = "man" Persion1. Age = 30 Persion1.id = 56341153 / / assign prison2 Persion2.name = "wangwu" Persion2.sex = "woman" Persion2.age = 18 Persion2.id = 78238232 / / use the function transfer structure printInfo (Persion1) fmt.Println ("-") printInfo (Persion2)} / / function to define the structure as a formal parameter Pass in func printInfo (p Persion) {fmt.Println ("name:", p.name) fmt.Println ("age:", p.age) fmt.Println ("gender:", p.sex) fmt.Println ("ID card:", p.id)}
The output is as follows
Name: lisi
Age: 30
Gender: man
ID card: 56341153
-
Name: wangwu
Age: 18
Gender: woman
ID card: 78238232
IV. Structural pointer
You can define pointers to structures that are similar to other pointer variables in the following format
Var struct_pointer * Persion
The pointer variable specified above can store the address of the structure variable, view the address of the structure variable, and place & match in front of the structure variable.
Struct_pointer = & Persion1
Use structure pointers to access structure members, you can use. Operator
Struct_pointer.title package main import "fmt" / / structure declares type Persion struct {name string age int sex string id int} func main () {/ / variable of structure type var (Persion1 Persion / / declares Persion1 as Persion type Persion2 Persion / / declares Persion2 as Persion type) / / assigns a value to prison1 Persion1.name = "lisi" Persion1.sex = "man" Persion1.age = 30 Persion1.id = 56341153 / / assign to prison2 Persion2.name = "wangwu" Persion2.sex = "woman" Persion2.age = 18 Persion2.id = 78238232 / / use the address of the function passing structure printInfo (& Persion1) fmt.Println ("-") printInfo (& Persion2)} / / define the structure Input func printInfo (p * Persion) {fmt.Println ("name:") P.name) fmt.Println ("age:", p.age) fmt.Println ("gender:", p.sex) fmt.Println ("ID card:", p.id)}
The output is as follows
Name: lisi
Age: 30
Gender: man
ID card: 56341153
-
Name: wangwu
Age: 18
Gender: woman
ID card: 78238232
This is the end of the content of "how to use the pointer of Go language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.