Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the pointer of Go language

Shulou Source: shulou.com Published: 2022-05-31 20:20:18 10月04日 Update

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!

Tags: Structure type variable pointer name age gender format identity ID card function member output result parameter address statement language code content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS vpn Shulou Tech Info Docker