Go logo

What is a pointer in Go Language?

  • Pointers in Go programming language is a variable which is used to keep the memory address of another variable.
  • Pointers in Golang can be termed as the special variable. The variables are utilized to save some data at a specific memory address in the system.
  • Like any constant or variable, you have to declare a pointer before you can use it to store any variable speech.

Declaring a pointer

var var_name *var-type

for example:

var intp *int       

var fltp *float32

Note: A newly declared pointer variable that has not yet been assigned to a variable has the nil value.

An example of a pointer

In this example, we simply get the address of a variable by using ‘&’:

package main




import "fmt"




func main() {

    x := 10

    fmt.Println("pointer address:", &x)

}

Output:

pointer address: 0xc42000e1f8

Assigning and displaying value of pointer example
package main




import "fmt"




func main() {




    var intX int = 35

   

    //Declaring a pointer variable 

    var ptrX *int

     

    ptrX = &intX







    fmt.Println("intX variable address = ", &intX)

    fmt.Println("Pointer variable ptrX value= ", ptrX)

    fmt.Println("Value of intX = ", intX)    

}

Output:

intX variable address =  0xc42000e1f8

Pointer variable ptrX value=  0xc42000e1f8

Value of intX =  35

Changing the value of pointer example

We will change the value of the pointer and display it:

package main 

import ("fmt") 

func main() { 

   ptrX := new(int) 

   fmt.Println("Before: ",*ptrX) 

   *ptrX = 35

   fmt.Println("Value of pointer after change: ",*ptrX) 

}

Output:

Before:  0

Value of pointer after change:  35

An example of an array with pointer

This example shows using array with pointer:

package main




import "fmt"







func main() {

const lim int = 3

   arr:= [3]int{10,20,30}

   var i int

   var ptr [lim]*int;




   for  i = 0; i < lim; i++ {

      ptr[i] = &arr[i]

   }

   for  i = 0; i < lim; i++ {

      fmt.Printf("Value of arr[%d] = %d\n", i,*ptr[i] )

   }

}

Output:

Value of arr[0] = 10

Value of arr[1] = 20

Value of arr[2] = 30

An example of passing pointers to function

In this example, we have declared two variables and assigned them values. Then we passed those to a function and swap the numbers using pointers:

package main




import ("fmt")




func main() {




   var numa int = 100

   var numb int= 200




   fmt.Printf("Value of numa before swap : %d\n", numa )

   fmt.Printf("Value of numb before swap : %d\n", numb )







   swapByPointer(&numa, &numb);

   fmt.Printf("\n\n" )       

   fmt.Printf("Value of numa after swap : %d\n", numa )

   fmt.Printf("Value of numb aftere swap : %d\n", numb )

}

func swapByPointer(numx *int, numy *int) {

   var temp int

   temp = *numx   

   *numx = *numy  

   *numy = temp   

}

 

Output:

Value of numa before swap : 100

Value of numb before swap : 200

 

Value of numa after swap : 200

Value of numb after swap : 100

Why we need pointers in Go?

To comprehend this need, first, we must understand the idea of variables. Variable are the names given to some memory location where the actual data is saved.

To access the saved data we want the address of that particular memory location. To remember all of the memory addresses (Hexadecimal Format) manually is an overhead that is why we use variables to store variables and data can be retrieved by simply using their name.

The Go also allows saving a hexadecimal number to a variable using the literal expression i.e. number beginning from 0x is a hexadecimal number.

With pointers, we could pass a reference to a variable (as an instance, as a parameter to a function), rather than passing a copy of the factor which may decrease memory usage and improve efficiency.