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

Golang Basic Tutorial

Golang Control Statements

Golang Functions & Methods

Golang Structs

Golang Slices & Arrays

Golang Strings(String)

Golang Pointers

Golang Interfaces

Golang Concurrency

Golang Exceptions(Error)

Golang Other Miscellaneous

Go Language Pointer

In the Go programming language, a pointer is a variable used to store the memory address of another variable. Pointers in Golang are also called special variables.VariableUsed to store some data at a specific memory address in the system. Always find memory addresses in hexadecimal format (starting with 0x, such as 0xFFAAF, etc.).

What is the role of pointers?
To understand this pointer, first, we must understand the concept of a variable. A variable is a name given to an actual data storage location. To access the stored data, we need the address of the specific storage location. Manually remembering all memory addresses (in hexadecimal format) is a cost, which is why we use variables to store data and can access variables simply by using the variable name.
Golang also allows the use of literal expressions to store hexadecimal numbers in variables, that is, from0xThe starting number is a hexadecimal number.

Example:In the following program, we will store hexadecimal numbers in variables. But you can see that the type of the value isintand save it as decimal, or in other wordsintThe decimal value of the type is being stored. However, the main point of this example is that we are storing hexadecimal values (considered as memory addresses), but it is not a pointer because it does not point to any other memory location of any other variable. It is just a user-defined variable. Therefore, we need to use pointers.

//Stored hexadecimal value
package main
import \
func main() {
    //Stored in hexadecimal
    //Value in variable
    x := 0xFF
    y := 0x9C
    // Display value
    fmt.Printf("The type of variable x is %T\n", x)
    fmt.Printf("The hexadecimal value of x is %X\n", x)
    fmt.Printf("The decimal value of x is %v\n", x)
    fmt.Printf("The type of variable y is %T\n", y)
    fmt.Printf("The hexadecimal value of y is %X\n", y)
    fmt.Printf("The decimal value of y is %v\n", y)
}

Output:

The type of variable x is int
The hexadecimal value of x is FF
The decimal value of x is 255
The type of variable y is int
The hexadecimal value of y is 9C
The decimal value of y is 156

Pointer is a special type of variable that not only stores the memory address of other variables but also points to the memory location and provides a method to find the value stored at that memory location. It is usually called a special type of variable because it is almost declared as a variable, but with*(deref operator).

Pointer declaration and initialization

Before we start, we will use two important operators in pointers, namely

*Operator Also known as the dereference operator, used to declare pointer variables and access the value stored at the address.

&operator Called the address operator, used to return the address of a variable or to access the pointer to the variable's address.

Declare a pointer:

var pointer_name *Data_Type

Example:Below is a string type pointer, which can only storeStringVariable's memory address.

var s *string

Pointer initialization:For this, you need to use the address operator to initialize the pointer with the memory address of another variable, as shown in the following example:

//Normal variable declaration
var a = 45
//Initialize pointer s
//Memory address of variable a
var s *int = &a
// Golang program to demonstrate declaration
//and pointer initialization
package main
import \
func main() {
    //Define a normal variable
    var x int = 5748
    //Pointer declaration
    var p *int
    //Initialize pointer
    p = &x
    //Display result
    fmt.Println("The value stored in x = ", x)
    fmt.Println("The memory address of x = ", &x)
    fmt.Println("The value stored in the variable p = ", p)
}

Output:

The value stored in x =  5748
The memory address of x = 0xc000066090
The value stored in the variable p = 0xc000066090

Important note:

  • The default or zero value of a pointer is always nil. Or, you can say that uninitialized pointers will always have a nil value.

    //Pointer's nil value
    package main
    import \
    func main() {
        //Define pointer
        var s *int
        // Display result
        fmt.Println("s = ", s)
    }

    Output:

    s = <nil>
  • The declaration and initialization of pointers can be completed in one line.

    var s *int = &a
  • If you want to specify both the data type and pointer declaration, the pointer will be able to handle the memory address of the specified data type variable. For example, if you use a string type pointer, then the address provided to the pointer variable will only be the address of a string data type variable, not any other type.

  • To overcome the above problem, you can usevar keywordThe concept of type inference. Data types do not need to be specified during declaration. The type of a pointer variable can also be determined by the compiler like a regular variable. Here, we will not use*operator. When we use another variable's address to initialize a variable, it will be internally determined by the compiler.

    // Golang program for demonstration
    //operator. When we initialize a variable with the address of another variable, it will be internally determined by the compiler.
    //Pointer variable
    package main
    import \
    func main() {
        //Using the var keyword
        //We have not defined
        //Any type with a variable
        var y = 458
        //Using a pointer variable
        // The var keyword without specifying
        //Type
        var p = &y
        fmt.Println("The value stored in y = ", y)
        fmt.Println("The address of y = ", &y)
        fmt.Println("The value stored in the pointer variable p = ", p)
    }

    Output:

    The value stored in y =  458
    The address of y = 0xc0000120a8
    The value stored in the pointer variable p = 0xc0000120a8
  • You can also useAbbreviation (:=)Syntax to declare and initialize pointer variables. If we use&(address)The operator passes the address of the variable to it, then the compiler will internally determine that the variable is a pointer variable.

    // Golang program for demonstration
    //Using shorthand syntax in
    //Pointer variable
    package main
    import \
    func main() {
        //Using the := operator for declaration
        //And initialize the variable
        y := 458
        //Using a pointer variable
        //Allocated by :=
        //The address of variable y
        p := &y
        fmt.Println("The value stored in y = ", y)
        fmt.Println("The address of y = ", &y)
        fmt.Println("The value stored in the pointer variable p = ", p)
    }

    Output:

    The value stored in y =  458
    The address of y = 0xc000066090
    The value stored in the pointer variable p = 0xc000066090

Pointer dereference

As is well known,*The operator is also called the dereference operator. It is not only used to declare pointer variables but also used to access the value stored in the variable pointed to by the pointer, and is usually calledIndirect or dereference.*The operator is also called the value at the addressLet's take an example to better understand this concept:

// Golang program example
//The concept of dereferencing a pointer
package main
import \
func main() {
    //Using the var keyword
    //We have not defined
    //Any type with a variable
    var y = 458
    //Using a pointer variable
    // The var keyword without specifying
    //Type
    var p = &y
    fmt.Println("The value stored in y = ", y)
    fmt.Println("The address of y = ", &y)
    fmt.Println("The value stored in the pointer variable p = ", p)
    //This is dereferencing the pointer
    //Used before the pointer*Operator
    //Variables are used to access the stored value
    //It points to the variable it points to
    fmt.Println("The value stored in y = ",*p) = ", *p)
}

Output:

The value stored in y =  458
The address of y = 0xc0000120a8
The value stored in the pointer variable p = 0xc0000120a8
The value stored in y =*p) =  458

You can also change the value of the pointer or the position in memory, rather than allocating a new value for the variable.

package main
import \
func main() {
    //Using the var keyword
    //We have not defined
    //The variable type is not specified
    var y = 458
    //Using a pointer variable
    // The var keyword without specifying a type
    var p = &y
    fmt.Println("The value stored in y before modification = ", y)
    fmt.Println("The address of y = ", &y)
    fmt.Println("The value stored in the pointer variable p = ", p)
    //This is dereferencing the pointer
    //Used before the pointer*Operator
    //Variables are used to access the stored value
    //It points to the variable it points to
    fmt.Println("The value stored before modification is in y(*The value in p) = ", *p)
    //Change the value of y by assignment
    //The new value of the pointer
    *p = 500
    fmt.Println("The value stored after modification is in y(*The value in p) = ", y)
}

Output:

The value stored in y before modification =  458
The address of y = 0xc0000120a8
The value stored in the pointer variable p = 0xc0000120a8
The value stored before modification is in y(*Value in (p)=  458
The value stored after modification is in y(*Value in (p)=  500