![]() |
VOOZH | about |
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.
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.
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:
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:
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:
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:
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:
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:
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