Explain To Me Like A Baby: Golang Pointers and receiver Method pt 1

Explain To Me Like A Baby: Golang Pointers and receiver Method pt 1

The concept of pointers and receiver methods can be quite confusing in Golang. In this article, I will explain what they are, why they are being used and how to use them.

What are pointers?

Pointers are a type of variable that is used to store the memory address of another variable. The variables are like the names given to the address (memory location).

Titi and Yemisi are friends. Yemisi needs to attend an event but she doesn't know the event centre nor the address of the event centre. She decided to go to Titi for help. Titi gave Yemisi the address of the event centre. Yemisi can now locate the event centre using the address given to her.

From the example above, we can describe the address of the event centre as the memory location/address of a variable and we can say the event centre building itself is the value of the variable.

Pointer Declaration And Dereferencing

Just like other data types, pointers can be declared using the var keyword. Dereferencing simply means retrieving the value stored in the memory location. The memory address is a hexadecimal number.

  • ***** Operator: it is used to declare pointer variables and also to access the values stored in the memory location

  • & Operator: it is used to access the address of a variable

Pointer Declaration

To declare a pointer variable without initializing it, we need to prefix the data type with ***** asterisk.


// Variable declaration without initialization
var variableName *DataType
var a *int

// Variable declaration with initialization i.e type inference
var num = 5
var a = &num

pointer-1.png

Accessing the values stored at the memory location (Dereferencing)

// Variable declaration without initialization
var variableName *DataType

var a *int

// Variable declaration with initialization i.e type inference
var num = 5
fmt.Println(num)
// prints out "5

var a = &num
fmt.Println(a)
// prints out "0xc000018030"

// get the value stored in the memory address
fmt.Println(*a)
// prints out 5
  • A variable can be assigned to another variable that is a memory address of another variable
package main

import "fmt"

func main() {
    //declare a variable
    var topic = "pointers and golang methods"

    // declare a new variable (topicAddress) that points to the memory address of topic
    var topicAddress = &topic

    fmt.Println("value of topic:", topic) 
    // = value of topic: pointers and golang methods

    fmt.Println("memory address of topic:", topicAddress) 
   // = memory address of topic: 0xc000010230

    // get the value stored in the memory address assigned to topicAddress
    fmt.Println("*topicAddress retrieves the value stored in the memory address assigned to it:", *topicAddress) 
    // = *topicAddress retrieves the value stored in 
    // the memory address assigned to it: pointers and golang methods

    // declare another variable that points to the memory address to topicAddress
    var newAddress = &topicAddress
    fmt.Println("newAddress points to the memory address of &topicAddress:", newAddress) 
    // = newAddress points to the memory address of &topicAddress: 0xc00000e028

    // get the value stored in the memory address assigned to newAddress
    //notice that you retrieve the memory address assigned to topicAddress
    // this is happened because we dereferenced newAddress only once
    fmt.Println("*newAddress retrieves the value stored in topicAddress:", *newAddress) 
    // = *newAddress retrieves the value stored in topicAddress: 0xc000010230

    // to get the value stored in variable topic, we have to dereference
    // our newAddress two times because it points first to the memory address of
    // topicAddress, and from above, our topicAddress is assigned the memory address
    // of topic
    fmt.Println("**newAddress returns the value stored in topic:", **newAddress) 
    // = **newAddress returns the value stored in topic: pointers and golang methods

}
  • More on Pointers

package main

import "fmt"

func main() {
    var a = 2
    fmt.Println(a)
    // = 2

    var b = *&a
    fmt.Println(b)
    // 2. The *& cancel each out and returns the value stored in variable a

    var c = &*a
    fmt.Println(c)
    // = invalid indirect of str (type string)

    var d = &&a
    fmt.Println(d)
    // = syntax error: unexpected &&, expecting expression

}

This article is my understanding of pointers based on my research and I hope it was helpful. If you have any questions or contributions, don't hesitate to drop your comments in the comment section.

REFERENCES