In this tutorial, we will study pointers in the Go programming language. In Golang, programming with pointers is pretty easy. As in any other language, a pointer concept is simple: it is used to point to the address of a memory location in which a variable needs to be accessed will reside & use “&” ampersand operator to address the memory location. You will learn how to declare and initialize a pointer in this tutorial.
A pointer is a composite data type or derived data type that stores the address of any variable stored in a memory location. A pointer holds or points to the memory address of another variable.
A pointer is a variable that is capable of storing the initial address of the object to which it wants to point to.
Let us briefly go through the concept of memory address & how variables are stored and accessed from the memory location.
Consider the memory of your computer which is capable of storing 20 bytes of information. Each block of memory is capable of storing one byte of information.
Let us assume that the starting address of this memory is 1,000 and the ending address is 1090. Suppose you want to store a variable of integer data type inside the memory. And the integer will take 2 bytes of memory i.e. it will take 2 blocks of memory.
Suppose a variable “p” is taken to store an integer. P stores a value of 100 which is assigned to it. In this example assume there is a pointer that points to the location where variable p is stored. Suppose “ptr” is the pointer that points to the base address or initial address of variable “p”.
Note: Pointing to means it simply stores the base address or initial address of the object. The object in our example is “p”’
Let’s see a simple program
package main
import "fmt"
func main() {
var a int = 100
fmt.Printf("Address of memory location of Variable a : %x\n", &a )
}
Output:
Address of memory location of Variable a : c000014050
The program shows the memory address where the variable a is stored ie, in memory location c000014050 the variable is stored.
Note: pointer is a special variable, not a normal variable that stores integer, character, and float. It will store the address or the base address of the variable
Pointers are used when
Syntax to declare a pointer
var <variable_name> *<variable_Type>
Where
var a *int /* pointer to an integer variable */
var c *float32 /* pointer to a float variable */
Note: - During declaration the initial value of a pointer is nil, ie by default the value of the pointer is set to zero.
package main
import "fmt"
func main() {
var a int= 100 /* actual variable declaration */
var Ptr *int /* pointer variable declaration */
Ptr = &a /* store address of a in pointer variable*/
fmt.Printf("Address of sampleVariable a value: %x\n", &a )
/* address stored in pointer variable */
fmt.Printf("Address stored in Pointr variable: %x\n", Ptr )
/* access the value using the pointer */
fmt.Printf("Value of *Pointr variable: %d\n", *Ptr )
}
Output:
Address of sampleVariable a value: c000014050 Address stored in Pointr variable: c000014050 Value of *Pointr variable: 100
Explanation
A pointer can be initialized in two ways
new
ambersand ‘&’
package main
import "fmt"
func main() {
c := new(int) // new operator
*c = 25
fmt.Println("Return the value stored in c \n" ,*c) //Output will be 25
fmt.Println("Return the address of c in memory \n", &c)
}
Output:
Return the value stored in c 25 Return the address of c in memory 0xc00000e030
package main
import "fmt"
func main() {
//Declare a pointer
var b *int
a := 9
b = &a
fmt.Println("return the address of a ",b)
fmt.Println("return the value stored in a",*b)
}
Output:
return the address of a 0xc000014050 return the value stored in a 9
New operator | & operator |
---|---|
c := new(int) *c = 25
|
var b *int a := 9 b = &a
|
Note:
The default value of a pointer once it is declared is nil i.e. zero. Let us see an example
package main
import "fmt"
func main() {
var Ptr *int //pointer declaration
fmt.Printf("The default Pointr value is nil : %x\n", Ptr ) //nil value ie 0
}
Output:
The default Pointr value is nil : 0
In the below-given program, a pointer variable b is declared. This is further assigned with the address of variable a. & an operation copies address of variable a to b. *b prints or gets the value in a.ie 9 in the below example.
Next, we modified b with value 3. so the memory location of b gets updated with the new value. The old value of b (9) gets modified to the latest declared value(3).
Check the program below for understanding the same.
package main
import "fmt"
func main() {
//Declare a pointer
var b *int
a := 9
b = &a
fmt.Println(a) // prints value of a
fmt.Println(*b) // * operator dereference prints value in b = 9
*b = 3
fmt.Println(a) //modified value in b with 3 b= 3
fmt.Println(*b) // prints b = 3
}
Output:
9 9 3 3
In Golang pointer to pointer can be created which of the below-given format,
a := 9
b = &a
c= &b
b and c are pointer variables,b holds the address of a. The pointer variable c holds the address of b, which actually stores the address of variable “a” with the value assigned as 9. In the above code c is a pointer to a pointer.
package main
import "fmt"
func main() {
a := 9
b := &a
c := &b
fmt.Printf("Value of a: %d\n", a)
fmt.Printf("address of a in b: %x\n", b)
fmt.Printf("pointer to pointer reference in c: %x\n", c)
fmt.Println()
fmt.Printf("value in variable a: %d\n", a)
fmt.Printf("*&a: %d\n", *&a) //value in variable a
fmt.Printf("*b: %d\n", *b)//value in variable a
fmt.Printf("**c: %d\n", **c)//value in variable a
fmt.Println()
fmt.Printf("displays address of a given &a: %d\n", &a)
fmt.Printf("b: %d\n", b)//displays address of a
fmt.Printf("&*b: %d\n", &*b)//displays address of a
fmt.Printf("*&b: %d\n", *&b)//displays address of a
fmt.Printf("*c: %d\n", *c)//displays address of a
fmt.Println()
fmt.Printf("b: %d\n", &b)
fmt.Printf("*c: %d\n", c)
}
Output:
Value of a: 9 address of a in b: c000014050 pointer to pointer reference in c: c00000e030 value in variable a: 9 *&a: 9 *b: 9 **c: 9 displays address of a given &a: 824633802832 b: 824633802832 &*b: 824633802832 *&b: 824633802832 *c: 824633802832 b: 824633778224 *c: 824633778224
From the given program it is clear that some of the operators produce the same output or are used in equivalent to some other same operations.
In order to display the value in variable a, a is assigned with a value of 9. Below given symbols are equivalent to producing the same output.
In the same way symbols equivalent to the display value of variable b which holds the address of a are
In the same way symbols equivalent to the display value of variable c which holds the address of a are