SoFunction
Updated on 2025-03-05

Detailed explanation of the usage of Go language learning pointer

introduction

In Go language, arrays can store data of the same type, but in the structure we can define different data types for different items.

A structure is a collection of data composed of a series of data of the same type or different types.

A structure represents a record, such as keeping a library's book record, each book has the following attributes:

Title: Title

Author: Author

Subject: Discipline

ID: Book ID

1. Define the structure

1. Syntax format

Structure definition requires the use of 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 variable declarations, and 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    (Persion{"zhangsan", "male", 20, 123412424})
    //Use key:value format    (Persion{name: "lisi", sex: "female", age: 18, id_card: 133654623})
    //The field ignored is 0 or empty    (Persion{name: "wangwu", id_card: 21352365})
}

Output result

{zhangsan male 20 123412424}
{lisi female 18 133654623}
{wangwu  0 21352365}

2. Access structure members

If you want to access structure members, you need to use the dot number. operator, the format is:

Structure. Member name"

Struct type variables are defined using the struct keyword, as follows:

package main
 
import "fmt"
 
func main() {
    //Structural Statement    type Persion struct {
        name string
        age  int
        sex  string
        id   int
    }
    //Structure type variable    var (
        Persion1 Persion //Declare Persion1 is Persion type        Persion2 Persion //Declare Persion2 as Persion type    )
 
    // Assign a value to *1     = "lisi"
     = "man"
     = 30
     = 56341153
 
    // Assign a value to *2     = "wangwu"
     = "woman"
     = 18
     = 78238232
 
    (Persion1)
    (Persion2)
}

The output result is as follows

{lisi 30 man 56341153}
{wangwu 18 woman 78238232} 

3. Structure as function parameters

Structures can also be passed to functions as parameters, which can solve some code redundancy problems and simplify the code.

Example

package main
 
import "fmt"
 
//Structural Statementtype Persion struct {
    name string
    age  int
    sex  string
    id   int
}
 
func main() {
 
    //Structure type variable    var (
        Persion1 Persion //Declare Persion1 is Persion type        Persion2 Persion //Declare Persion2 as Persion type    )
 
    // Assign a value to *1     = "lisi"
     = "man"
     = 30
     = 56341153
 
    // Assign a value to *2     = "wangwu"
     = "woman"
     = 18
     = 78238232
 
    //Use function to pass the structure    printInfo(Persion1)
    ("------------------")
    printInfo(Persion2)
}
 
//The function defines the structure as a formal parameter and passes infunc printInfo(p Persion) {
    ("Name: ", )
    ("age: ", )
    ("gender: ", )
    ("ID card: ", )
}

The output result is as follows

Name: lisi
Age:  30
Gender: man
ID card:  56341153
------------------
Name: wangwu
Age: 18
Gender: woman
ID card:  78238232

4. Structural pointer

You can define a pointer to a structure similar to other pointer variables, in the format as follows

var struct_pointer *Persion

The above-determined pointer variable can store the address of the structure variable, view the address of the structure variable, and place the & conformance in front of the structure variable

struct_pointer = &Persion1

Use the structure pointer to access the structure members, you can use the . operator

struct_pointer.title
package main
 
import "fmt"
 
//Structural Statementtype Persion struct {
    name string
    age  int
    sex  string
    id   int
}
 
func main() {
 
    //Structure type variable    var (
        Persion1 Persion //Declare Persion1 is Persion type        Persion2 Persion //Declare Persion2 as Persion type    )
 
    // Assign a value to *1     = "lisi"
     = "man"
     = 30
     = 56341153
 
    // Assign a value to *2     = "wangwu"
     = "woman"
     = 18
     = 78238232
 
    //Use function to pass the address of the structure    printInfo(&Persion1)
    ("------------------")
    printInfo(&Persion2)
}
 
//Define the structure pointer parameter for incomingfunc printInfo(p *Persion) {
    ("Name: ", )
    ("age: ", )
    ("gender: ", )
    ("ID card: ", )
}

The output result is as follows

Name: lisi
Age:  30
Gender: man
ID card:  56341153
------------------
Name: wangwu
Age: 18
Gender: woman
ID card:  78238232

Summarize

Structures are data sets composed of the same type and different types, and the stored structure types are unrestricted.

The above is a detailed explanation of the usage of Go language learning pointers. For more information about Go language pointers, please pay attention to my other related articles!