Explain Pointers in Go Language | Go Programming Tutorial
Explain Pointers in Go Language | Go Programming Tutorial
In this article, you will learn about:
- What are Pointers?
- What is the need for the Pointers?
- How to Declare and Initialize the Pointers?
- Important Points about Pointers
What are Pointers?
In GoLang, the pointers are the variables used in storing the memory address of another variable. Pointers in GoLang have also termed the special variables. The variables are used in storing the data at a particular location in the system. The memory addresses are always in hexadecimal format.
What is the need for the pointers?
To get the concept of pointers, we have to understand the concept of variables. Variables are then given to a memory location where the data is stored. We need to have access to stored data of the particular memory location. Remember all the memory locations are a little tricky, that’s why we use the variable to capture data, and variables are accessed by using the name.
How to Declare and Initialize the Pointers?
The important operators which we will use in pointers are:
- *Operator: It is also known as the dereferencing operator used to declare a pointer variable and access the value stored in the location.
- & operator: It is also known as the address operator used to return the address of a variable. Or in other words, It is used to access the address of a variable to a pointer.
Pointer Declaration:
var pointer_name *Data_type
Pointer Initialization:
var i *int = &p
Important Points about Pointer
- The zero value and the default value of a pointer are always considered nil. Or in other words, the uninitialized pointer will always have a nil value.
- The declaration and initialization of the pointers can be performed into a single line.
var i *int = &p.
- If the pointer is declared with some specific data type then it will handle the data of a similar data type only.
- To overcome the above problem use the Type Inference concept of the var keyword. There is no need to mention the type of variable during the declaration.