Golang Working with pointers

Golang Working with pointers

Note: To execute golang code online you can use https://play.golang.org/

What is a pointer?

  • A pointer is a variable which holds the memory address of another variable.

When to use a pointer?

  • In golang we when using variables the variable passed as a value.
  • In some cases, we want to modify the data of a variable, for example we are passing a struct variable and want to update a field value. we can't do it because we are passing the value of a variable.
  • For this kind of cases we use pointers and update the data of a variable.
  • As we are not passing the value so, we are not using the extra bit of memory. By using pointers we can utilize the memory effectively.

Representing pointer in golang

  • We represent the pointers with *T where T is a name of pointer.
  • Let's write a simple program to see how to implement the pointers.
package main

import "fmt"

func main(){
    FullName := "Anjaneyulu Batta";
    var Name *string = &FullName;
    fmt.Printf("Type of Name is %T\n", Name)
    fmt.Println("Address of Name is", Name)
    // ouput: Type of Name is *string
    //        Address of Name is 0xc00010a220
}

Accessing value of a pointer in golang

To get the value of a pointer we can simply use a * in fron of the pointer variable. Let's see an example below

package main

import "fmt"

func main(){
    FullName := "Anjaneyulu Batta";
    var Name *string = &FullName;
    fmt.Println("Value of pointer Name is ", *Name)
    // ouput: Value of pointer Name is  Anjaneyulu Batta
}

Passing pointer to a function

Let's see a simple example to update the value of a pointer of type map.

package main

import "fmt"

type Employee struct{
    Name string
    ID   string
}

func UpdateEmployeeWithPointer(Emp *Employee){
    Emp.Name = "John"
    Emp.ID = "EMP002"
}

func UpdateEmployeeWithoutPointer(Emp Employee){
    Emp.Name = "John"
    Emp.ID = "EMP002"
}

func main(){
    emp := Employee{Name: "Anji", ID: "EMP001"}
    var empPointer *Employee = &emp;
    // call function UpdateEmployeeWithoutPointer
    UpdateEmployeeWithoutPointer(emp)
    fmt.Println(emp.Name, emp.ID)
    // output:  Anji EMP001

    // call function UpdateEmployeeWithPointer
    UpdateEmployeeWithPointer(empPointer)
    fmt.Println(emp.Name, emp.ID)
    // output:  John EMP002
}

Returning pointer from a function

It is perfectly legal for a function to return a pointer of a local variable. The Go compiler is intelligent enough and it will allocate this variable on the heap.

package main

import (  
    "fmt"
)

func returnPointer() *int {  
    i := 5
    return &i
}
func main() {  
    d := returnPointer()
    fmt.Println("Value of d", *d)
    // ouput: Value of d 5
}

Note: We don't need to work with array pointers as it's internally uses the pointer concept.

using slices for arrays instead pointers

Let's see a simple example

package main

import (  
    "fmt"
)

func modify(sls []int) {  
    sls[0] = 90
}

func main() {  
    a := [3]int{89, 90, 91}
    modify(a[:])
    fmt.Println(a)
    // [90 90 91]
}

We have covered simple cases, we can do more with pointers. But, be cautious when using pointers because it modifies the data.