Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pointers
go
memory
communitycreator

What are pointers in Golang?

Vafa Batool

In Golang, a pointer is a variable that stores the address of an object stored in memory. All the variables that contain data of different types, such as int or string, are stored in memory, and a pointer “points” to these variables. These memory addresses are in the hexadecimal format, for example, 0x11AA3.

Note: In Golang, pointers are known as special variables.

Let’s understand pointers through a diagram:

Pointers "a" and "b" store the memory locations of "int" and "string" type variables

The illustration above shows two variables of type int and string stored in the memory. The int variable stores a value of 10, while the string variable stores the value “educative”. Two pointers, a and b, point towards the memory location of the respective variables, that is, they store the addresses.

Declaring a pointer

There are two important operators that are associated with pointers:

  • * Operator: This is also called a dereferencing operator. It is used for the declaration of a pointer, as well as for accessing the value stored at the address that the pointer is pointing to.
  • & Operator: This is known as an address operator. It is used to get the address of a variable.

The syntax for declaring a pointer in Golang is:

var name *Type
  • name: This is the name of the pointer variable.

  • Type: This is the data type of the variable that the pointer points to.

Let’s look at an example of a pointer of type string:

var myPointer *string

Initialization of a pointer

The initialization of a pointer requires the use of the address operator to store the memory address of a variable in the pointer.


// variable whose address the pointer will store 
var intData = 15 

//initialization of a pointer of int type that stores
//the memory address of intData
var intPointer *int = intData

Code

Let’s look at a code example for the initialization of a pointer and the manipulation of values of variables whose addresses the pointer stores:

package main
import "fmt"
/* For exercises uncomment the imports below */
// import "strconv"
// import "encoding/json"

func main() {

    // a normal variable  whose address the pointer will store 
    var intData = 20 
    
    //declaration of a pointer 
    var intPointer *int

    //intPointer now points towards intData    
    intPointer = &intData 

    fmt.Println("what intData stores:", intData)
    fmt.Println("address of intData:", &intData)
    fmt.Println("what intPointer stores:", intPointer)


    //this updates the value of intData using dereferncing operator 
    *intPointer = 30

    fmt.Println("what intData now stores:", intData)

}
Use of pointers in Golang

Code explanation

  • Lines 10–16: We initialize a pointer by storing the address of a variable in the pointer.

  • Line 20: We use the derefering pointer to manipulate the value of the variable that the pointer points to. Since the pointer directly points to the memory location of the variable, the actual value of the variable is altered.

RELATED TAGS

pointers
go
memory
communitycreator
RELATED COURSES

View all Courses

Keep Exploring