DEV Community

Cover image for Pointers in Golang
kennethnnah
kennethnnah

Posted on

Pointers in Golang

The concept of pointers in golang is one fundamental topic that should be taken very seriously hence the reason for this post. As a beginner, you may have seen the use of ampersand"&" and asteriske"*" symbols which we call pointers.

A pointer is a special variable that is used to store data at a particular memory address (Variable Name = Hex Address). The memory address comes in the form of a hexadecimal format(starting with 0x like 0x50c108 etc.)

Before we get started explaining the meaning of pointers, We shall discuss what variables mean. A variable is the name of a memory location where data is stored, to access the stored data we need to know the location or address where that data is stored.

Skip this paragraph if you understand the previous. If you dont understand, no worries as I bring home the explanation. 'abia' is a variable name that holds a memory address (0x50c108) which stores data, the memory address is a Hex code which is not easily remembered by humans so we use a named readable convention like "abia" to represent the address in memory where the data "nnamdi" recides. To locate "nnamdi", we have to know the memory address or the variable that holds the value "nnamdi"

Why do we need pointers in our program?

We are going to demostrate this further with an example, in this example we are going to store a hexadecimal number inside variables a and b, the value type is an int. The code snippet below is not a pointer as it is not pointing to a memory location of another variable, it only stores hexadecimal values to a and b as seen below.


// Variables storing the hexadecimal values 

package main

import "fmt"

func main() {

    // storing the hexadecimal
    // values in variables
    a := 0xFF
    b := 0x9C

    // Printing out variable values 
    fmt.Printf("Type of variable a is %T\n", a)
    fmt.Printf("Value of a in hexadecimal is %X\n", a)
    fmt.Printf("Value of a in decimal is %v\n", a)

    fmt.Printf("Type of variable b is %T\n", b)
    fmt.Printf("Value of b in hexadecimal is %X\n", b)
    fmt.Printf("Value of b in decimal is %v\n", b)   

}
Enter fullscreen mode Exit fullscreen mode

Output:
Type of variable a is int
Value of a in hexadecimal is FF
Value of a in decimal is 255
Type of variable b is int
Value of b in hexadecimal is 9C
Value of b in decimal is 156

A pointer does not only point to the location or address of another variable but also finds out way to get the value where that variable is located in memory.You may want to ask if the location of a variable is different from the address of that same variable, the answer is No? they are the same, the variable name is just an easy way to represent long hexadecimal addressing scheme.

Declaring and Innitializing Pointers

We will look into the 2 important keyword for this blog post which are:

  • Operator: This symbol is called the dereferencing operator used to declare a pointer variable and access the value stored in the address.

Below is an example of a pointer declaration which stores the value of the address of another variable.

Example pointer declaration

 var y string = "String Y"
//Innitialized x with memory address of y
 x := &y
 fmt.Printf("Prints the value of y", *x) // String Y
 fmt.Printf("Prints the address of x", &x) // 0xc000100018
 fmt.Printf("Prints the address of y", x) // 0xc000010230   
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)