Google News
logo
Golang - Interview Questions
What are Golang pointers?
Go Pointers are those variables that hold the address of any variables. Due to this, they are called special variables. Pointers support two operators:
 
 *  operator : This operator is called a dereferencing operator and is used for accessing the value in the address stored by the pointer.

 &  operator : This operator is called the address operator and is used for returning the address of the variable stored in the pointer.
This is illustrated in the diagram below. Here, consider we have a variable x assigned to 100. We store x in the memory address 0x0201. Now, when we create a pointer of the name Y for the variable x, we assign the value as  &x  for storing the address of variable x. The pointer variable is stored in address 0x0208. Now to get the value stored in the address that is stored in the pointer, we can just write int z:= *Y

Pointers are used for the following purposes :
 
* Allowing function to directly mutate value passed to it. That is achieving pass by reference functionality.

* For increasing the performance in the edge cases in the presence of a large data structure. Using pointers help to copy large data efficiently.

* Helps in signifying the lack of values. For instance, while unmarshalling JSON data into a struct, it is useful to know if the key is present or absent then the key is present with 0 value.

Advertisement