English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statement

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String (String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception (Error)

Golang Miscellaneous

Go Pointer Points to Pointer (Double Pointer)

In the Go programming language, a pointer is a variable used to store the memory address of another variable. A pointer is a special variable, so it can even point to any type of variable. Basically, it looks like a pointer chain. When we define a pointer to a pointer, the first pointer is used to store the address of the second pointer. This concept is sometimes calledDouble pointers.

How to declare a pointer to pointer in Golang?
To declare a pointer as Pointer similar to declaring a pointer in Go. The difference is that we must place an additional' * 。 Usually, when we usevar keywordAnd type declaration when declaring pointer variables, you can complete this operation.

Let's make some changes to the above program. Assign some new values to the pointer by using dereferencing to change the pointer's value, as shown below:1The value at the address is) orIn the following program, the pointerpt2Storingpt1The address of the pointer. Dereferencingpt2That is* pt2It will give the variablevThe address, or you can also say pointerpt1The value. If you try** pt2,Then it will give the variablevThe value, namely100.

This Go program is used to illustrate the concept of pointer to pointer
package main
import \
    //An integer type variable
    var V int = 100
    //Get a pointer
    //The integer type
    var pt1 *int = &V
    //int = &v
    //Get the pointer pointed to1The pointer address pointing to pt
    //Store the address of pt1Converted to pt2
    var pt2 **int = &pt1
    fmt.Println("The value of variable V = ", V)
    fmt.Println("The address of variable V = ", &V)
    fmt.Println("pt1The value =1=
    fmt.Println("pt1The address =1=
    fmt.Println("pt2The value =2=
    //Dereferencing
    //Pointer to pointer
    fmt.Println("pt2c0*pt2) = *pt2=
    //Double pointers will give the value of variable V
    fmt.Println("*) = 0xc00002(pt **pt2 ", **pt2=
}

Output:

)  100
The value of variable V =62090
pt1The address = 0xc000062090
pt1The address of variable V = 0xc00008The value = 0xc000018
pt2The address = 0xc00008The value = 0xc000018
pt2c0*pt2The value at the address is (62090
*) = 0xc00002(pt **pt2 =  100

Let's make some changes to the above program. Assign some new values to the pointer by using dereferencing to change the pointer's value, as shown below:2The value at the address is) or:

// Example
This Go program is used to illustrate the concept of pointer to pointer
package main
import \
    // func main() {
    Define an int type variable 100
    // var v int =
    var pt1 *A pointer of an integer type
    //int = &v
    //Get the pointer pointed to1The pointer address pointing to pt
    //Store the address of pt1Converted to pt2
    var pt2 **int = &pt1
    fmt.Println("The value of variable v is = ", v)
    //By assignment to change the value of v
    //Pointing to the pointer pt1to the new value
    *pt1 = 200
    fmt.Println("Change pt1The value stored in v = "v"
    //By assignment to change the value of v
    //Pointing to the pointer pt2to the new value
    **pt2 = 300
    fmt.Println("Change pt2The value stored in v = "v"
}

Output:

The value of variable v is =  100
Change pt1The value stored in v =  200
Change pt2The value stored in v =  300

Related Knowledge:Go Pointer