Introduction
Pointers is Go is one of the most complex concepts especially for beginners. In this blog, I have explained the Pointers in a very simple format through visualization and point-by-point explanation. Please read the full blog for complete understanding. To start with let me give you the definition of Pointers:
Pointer stores the memory address of the variable instead of the value of a variable.
This makes it different from other types of variables.
Pass By Value
Let me explain to you the concept of Pass By Value
before going deep in understanding Pointers. If you understand this then It will help you in understanding the code with extensive use of Pointers. In Pass By Value
the value of a variable is copied into the calling method argument instead of the reference or memory address of the variable. Just remember this simple formula:
Go always follows Pass By Value and not Pass By Reference
Let's see How Pass By Value
works in Go.
- Variable
x
of type string is defined and the value "Sagar" is assigned to it. - Method
addSurname()
is called withx
, Here the value ofx
that is "Sagar" is copied to variabley
- String "Jadhav" is appended to the value of
y
and the result is assigned back toy
.
So updating the value of y
doesn't update the value of x
as both are different variables but storing the same value.
Pointer in Action
In the above example How to change the value of x
by updating the value of y
? Here Pointers come into the action. Yes, It is possible to update the value of one variable through other variables with the help of Pointers. Let me explain you through the below example:
- Variable
x
of type string is defined and the value "Sagar" is assigned to it. - Variable
y
of type string pointer is defined and the address ofx
is assigned to it. - Method
addSurname()
is called withy
, Here the value ofy
that is the address ofx
is copied to variablez
. - String "Jadhav" is appended to the value at address stored in
z
and the result "Sagar Jadhav" is assigned back to the variable at the address stored inz
. Sounds complicated, Map this point with the above picture for better understanding.
Pointer Receiver
The use of Pointer with Structs is very common, Hence understanding this concept is a must. Let's get deep-dive through the below example:
- Variable
emp
of type employee struct is defined and the propertyName
is initialized with value "Sagar". - Method Receiver
addTitle()
is called with string "Mr.", Here the string "Mr." is copied to variablet
. - Value of
Name
property is appended to Value oft
and the result "Mr. Sagar" is assigned back to theName
property. But here theName
property ofemp
is not updated becauseaddTitle()
is a method receiver so when it is called, Value ofemp
variable is copied to another variablee
. So any updatee
will not affectemp
. - Pointer receiver
addSurname()
is called with string "Jadhav", Here the string "Jadhav" is copied to variables
. - Value of
s
is appended to the value ofName
property and the result "Sagar Jadhav" assigned back to theName
property. Hereemp
variable is updated because variablee
is a pointer toemp
so any changes ine
will also affectemp
.
Checkout here for more details.
Top comments (0)