Simple Memory Address & Pointers In Golang-2019

In below article you will understand Memory Address, Pointers and Pass By Value and Pass By Reference.

Check This Previous blog post written by Sagar Jaybhay for better understanding.

­Memory Address In Golang:

When you declared any kind of variable in golang, Go compile allocate some memory for it and this based on the type of that variable and memory is allocated on RAM.

Go compiler allocates a specific size of memory to store the data. When this memory is allocated means it has some memory address.

Memory address for what? To find variable value need memory address. Memory addresses generally represented in hexadecimal values.

Go language you can easily access the memory address of variable by using &(and) operator. To use this you need to write &variable_name then it will print memory address for this.

Hexadecimal value looks like this 0xc00002df38

Below is a program that print value of variable along with memory address

package main

func main() {

    var i = 10
    var j = "sagar jaybhay"

    println("i value  =>", i, " memory address of i =>", &i)
    println("j value  =>", j, " memory address of j =>", &j)
}

Memory Address in Golang
Memory Address in Golang

Pointers In golang:

In Go programming language or golang, it is a variable that is used to store the memory address of another variable. It is a special kind of variable. The memory address is in hexadecimal format.

So what is the need for pointers

To understand this we need to understand the concept of variable. Variable means a name given to memory location where our actual data is present. So to access the stored data we need the address of that memory location and we know the memory address is in a hexadecimal format so it is hard to remember. Also, golang provides a cool feature that we can store hexadecimal value to a variable.

A pointer is special kind of variable which is stored the memory address of other variable and also points to the memory where the memory is located and also provides the way to find the value of that variable or which is stored in that memory location.

It is called a special kind of variable because it declared almost any kind of variable but with * this star also called a dereferencing operator.

‘*’ operator: this operator also called a dereferencing operator which used to return the address of the variable. So this operator not only used to declare a pointer but also used to access the value of that memory address holds.

‘&’ operator: is termed as address operator and this used to return the address of a variable.

How to declare pointer in golang?

Var pointer_name *Data_Type

How to initialize the variable?

var a=50
var s * int =&a

Points to remember:

  • The default value of a pointer is nil also termed as an uninitialized pointer.
  • If you specify the data type at the time of pointer declaration then it handles the memory address of that variable only and if you try to assign any other datatype variable.
  • To overcome the above problem you need to use type inference concept means var keyword. There is no need to declare datatype at the time of declaration of a pointer by doing this compiler will decide at run time what type it is like any other variable.
var v=10
var p=&v

you can also use shorthand syntax to declare a pointer.

Y:=10
J:=&Y

To understand better we need to understand pass by reference and pass by value.

Pass By Value:

When you call a function and pass variable as argument then it is passed by value what it means if you pass x variable to function ABC and ABC function do some manipulation on x variable then the actual value of x is not changed what it means when function is called a function create another self copy of that variable for itself. See below example

package main

func main() {

    x := 10
    returnbyvalue(x)
    println("value of x in main method", x,
        "  memory address of x in main method ", &x)

}

func returnbyvalue(x int) {
    x = x * 10
    println("value of x in returnbyvalue method", x,
        "  memory address of x in returnbyvalue method ", &x)
}

Pass By Value In Golang
Pass By Value In Golang

Pass By Reference

When you pass a parameter by memory address and do some manipulation on that variable then these manipulations are performed on the same copy of that base variable. Called function not create a copy of that variable for local reference callee and called function both use the same copy of that variable so changes in called function also affect callee function see below example

package main

func retunbyreference(x *int) {
    *x = (*x) * 10
    println("value of x in retunbyreference method", *x,
        "  memory address of x in retunbyreference method ", &(*x))

}

func main() {

    x := 10
    retunbyreference(&x)
    println("value of x in main method", x,
        "  memory address of x in main method ", &x)

}

Pass By Referance In Golang
Pass By Referance In Golang

GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...