Pointers in Golang

11 Aug, 2021
Pointers in Go programming language or Golang is a variable that 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 for 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. 
Go
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
default, selected
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
// Golang program to demonstrate the variables// storing the hexadecimal valuespackage 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 hexadecimal 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 hexadecimal 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 hexadecimal is FF
Value of x in decimal is 255
Type of variable y is int
Value of y in hexadecimal is 9C
Value of y in decimal is 156
A pointer is a special kind of variable that is not only used to store the memory addresses of other variables but also points where the memory is located and provides ways to find out the value stored at that memory location. It is generally termed as a Special kind of Variable because it is almost declared as a variable but with *(dereferencing operator). 
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:
Go
// Golang program to demonstrate the declaration// and initialization of pointerspackage 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
1. 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:
Go
// Golang program to demonstrate// the nil value of the pointerpackage main import "fmt" func main() {         // taking a pointer    var s *int         // displaying the result    fmt.Println("s = ", s)}
Output:  
s =  <nil>
2. Declaration and initialization of the pointers can be done into a single line.
Example: 
var s *int = &a
3. 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.
4. To overcome the above mention problem you can use the Type Inference concept of the var keyword. There is no need to specify the data type 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:
Go
// Golang program to demonstrate// the use of type inference in// Pointer variablespackage 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)}
Output:  
Value stored in y =  458
Address of y =  0x414020
Value stored in pointer variable p =  0x414020
5. 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 the variable using &(address) operator to it.
Example:
Go
// Golang program to demonstrate// the use of shorthand syntax in// Pointer variablespackage main import "fmt" func main() {     // using := operator to declare    // and initialize the variable    y := 458         // taking a pointer variable using    // := by assigning it with the    // address of variable y    p := &y     fmt.Println("Value stored in y = ", y)    fmt.Println("Address of y = ", &y)    fmt.Println("Value stored in pointer variable p = ", p)}
Output:
Value stored in y =  458
Address of y =  0x414020
Value stored in pointer variable p =  0x414020
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: 
Go
// Golang program to illustrate the// concept of dereferencing a pointerpackage 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: 
Go
// Golang program to illustrate the// above mentioned conceptpackage 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
sagartomar9927
YashPathak
Golang
Golang-Pointers
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to concatenate two strings in Golang
time.Sleep() Function in Golang With Examples
strings.Contains Function in Golang with Examples
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Golang Maps
Time Formatting in Golang
Interfaces in Golang
Different Ways to Find the Type of Variable in Golang
How to convert a string in lower case in Golang?