Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.).
What is the need of the pointers?
To understand this need, first, we have to understand the concept of variables. Variables are the names given to a memory location where the actual data is stored. To access the stored data we need the address of that particular memory location. To remember all the memory addresses(Hexadecimal Format) manually is an overhead that’s why we use variables to store data and variables can be accessed just by using their name.
Golang also allows saving a hexadecimal number into a variable using the literal expression i.e. number starting from 0x is a hexadecimal number.
Example: In the below program, we are storing the hexadecimal number into a variable. But you can see that the type of values is int and saved as the decimal or you can say the decimal value of type int is storing. But the main point to explain this example is that we are storing a hexadecimal value(consider it a memory address) but it is not a pointer as it is not pointing to any other memory location of another variable. It is just a user-defined variable. So this arises the need for pointers.
// Golang program to demonstrate the variables // storing the hexadecimal values package main import "fmt" func main() { // storing the hexadecimal // values in variables x := 0xFF y := 0x9C // Displaying the values fmt.Printf("Type of variable x is %T\n", x) fmt.Printf("Value of x in hexdecimal is %X\n", x) fmt.Printf("Value of x in decimal is %v\n", x) fmt.Printf("Type of variable y is %T\n", y) fmt.Printf("Value of y in hexdecimal is %X\n", y) fmt.Printf("Value of y in decimal is %v\n", y) } |
Output:
Type of variable x is int Value of x in hexdecimal is FF Value of x in decimal is 255 Type of variable y is int Value of y in hexdecimal is 9C Value of y in decimal is 156
A pointer is a special kind of variable which is not only used to store the memory addresses of other variables but it also points where the memory is located and provides the ways to find out the value stored at that memory location. It is generally termed as Special kind of Variable because it is almost declared as a variable but with *(dereferencing operator).
Declaration and Initialization of Pointers
Before we start there are two important operators which we will use in pointers i.e.
* Operator also termed as the dereferencing operator used to declare pointer variable and access the value stored in the address.
& operator termed as address operator used to returns the address of a variable or to access the address of a variable to a pointer.
Declaring a pointer:
var pointer_name *Data_Type
Example: Below is a pointer of type string which can store only the memory addresses of string variables.
var s *string
Initialization of Pointer: To do this you need to initialize a pointer with the memory address of another variable using the address operator as shown in the below example:
// normal variable declaration var a = 45 // Initialization of pointer s with // memory address of variable a var s *int = &a;
Example:
// Golang program to demonstrate the declaration // and initialization of pointers package main import "fmt" func main() { // taking a normal variable var x int = 5748 // declaration of pointer var p *int // initialization of pointer p = &x; // displaying the result fmt.Println("Value stored in x = ", x) fmt.Println("Address of x = ", &x;) fmt.Println("Value stored in variable p = ", p) } |
Output:
Value stored in x = 5748 Address of x = 0x414020 Value stored in variable p = 0x414020
Important Points:
- The default value or zero-value of a pointer is always nil. Or you can say that an uninitialized pointer will always have a nil value.
Example:
// Golang program to demonstrate// the nil value of the pointerpackage mainimport"fmt"func main() {// taking a pointervars *int// displaying the resultfmt.Println("s = ", s)}chevron_rightfilter_noneOutput:
s = <nil>
- Declaration and initialization of the pointers can be done into a single line.
Example:
var s *int = &a;
- If you are specifying the data type along with the pointer declaration then the pointer will be able to handle the memory address of that specified data type variable. For example, if you taking a pointer of string type then the address of the variable that you will give to a pointer will be only of string data type variable, not any other type.
- To overcome the above mention problem you can use the Type Inference concept of var keyword. There is no need to specify the data type of during the declaration. The type of a pointer variable can also be determined by the compiler like a normal variable. Here we will not use the * operator. It will internally determine by the compiler as we are initializing the variable with the address of another variable.
Example:
// Golang program to demonstrate// the use of type inference in// Pointer variablespackage mainimport"fmt"func main() {// using var keyword// we are not defining// any type with variablevary = 458// taking a pointer variable using// var keyword without specifying// the typevarp = &y;fmt.Println("Value stored in y = ", y)fmt.Println("Address of y = ", &y;)fmt.Println("Value stored in pointer variable p = ", p)}chevron_rightfilter_noneOutput:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020
- You can also use the shorthand (:=) syntax to declare and initialize the pointer variables. The compiler will internally determine the variable is a pointer variable if we are passing the address of variable using &(address) operator to it.
Example:
// Golang program to demonstrate// the use of shorthand syntax in// Pointer variablespackage mainimport"fmt"func main() {// using := operator to declare// and initialize the variabley := 458// taking a pointer variable using// := by assigning it with the// address of variable yp := &y;fmt.Println("Value stored in y = ", y)fmt.Println("Address of y = ", &y;)fmt.Println("Value stored in pointer variable p = ", p)}chevron_rightfilter_noneOutput:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020
Dereferencing the Pointer
As we know that * operator is also termed as the dereferencing operator. It is not only used to declare the pointer variable but also used to access the value stored in the variable which the pointer points to which is generally termed as indirecting or dereferencing. * operator is also termed as the value at the address of. Let’s take an example to get a better understandability of this concept:
Example:
// Golang program to illustrate the // concept of dereferencing a pointer package main import "fmt" func main() { // using var keyword // we are not defining // any type with variable var y = 458 // taking a pointer variable using // var keyword without specifying // the type var p = &y; fmt.Println("Value stored in y = ", y) fmt.Println("Address of y = ", &y;) fmt.Println("Value stored in pointer variable p = ", p) // this is dereferencing a pointer // using * operator before a pointer // variable to access the value stored // at the variable at which it is pointing fmt.Println("Value stored in y(*p) = ", *p) } |
Output:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020 Value stored in y(*p) = 458
You can also change the value of the pointer or at the memory location instead of assigning a new value to the variable.
Example:
// Golang program to illustrate the // above mentioned concept package main import "fmt" func main() { // using var keyword // we are not defining // any type with variable var y = 458 // taking a pointer variable using // var keyword without specifying // the type var p = &y; fmt.Println("Value stored in y before changing = ", y) fmt.Println("Address of y = ", &y;) fmt.Println("Value stored in pointer variable p = ", p) // this is dereferencing a pointer // using * operator before a pointer // variable to access the value stored // at the variable at which it is pointing fmt.Println("Value stored in y(*p) Before Changing = ", *p) // changing the value of y by assigning // the new value to the pointer *p = 500 fmt.Println("Value stored in y(*p) after Changing = ",y) } |
Output:
Value stored in y before changing = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020 Value stored in y(*p) Before Changing = 458 Value stored in y(*p) after Changing = 500
Recommended Posts:
- Comparing Pointers in Golang
- Pointers to a Function in Go
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- Import in GoLang
- time.Round() Function in Golang With Examples
- How to add a method to struct type in Golang?
- Converting a string variable into Boolean, Integer or Float type in Golang
- Check if the given slice is sorted in Golang
- How to compare times in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


