Table of contents
1.
Introduction
2.
Pointers in Go Programming
2.1.
Different Types of Pointers in Go 
3.
Comparing Pointers in GoLang
4.
Using == Operator
5.
Using != operator
6.
Frequently Asked Questions
6.1.
What is Golang used for?
6.2.
Is it possible to compare two pointers in Go?
6.3.
In Golang, what does a pointer do?
6.4.
What are the different sorts of pointers in Go?
6.5.
In Golang, how big is a pointer?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Comparing Pointers in Go

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Google engineers have introduced a statically typed, compiled programming language most closely modelled after C known as Go (also known as Golang or Go language). It is an open-source general-purpose computer language that is procedural and concurrent.

So, what’s new in this article?

Today, we’ll be looking at how to compare pointers in Go to clear your concepts in detail. 

Let’s get started then:

Pointers in Go Programming

A pointer is a variable in Golang or any programming language which stores the memory address of other variables. In Golang, pointers are also called as special variables which are used to store data in the system at a specific memory address. In Go, the pointer type is used to point to a location in memory where data is kept. 

The variable pointer is shown in the below figure.

Different Types of Pointers in Go 

In Go, the pointer type is used to point to a location in memory where data is kept. Go uses the * operator to designate a type as a pointer, similar to C/C++. The example below displays many pointers with various underlying types

var ptr1 *int
var ptr2 *float32
var row [ ]*int64
var column [ ]*int32
var matrix *[1024]int

 

Comparing Pointers in GoLang

In the Go programming language, you can compare two pointers to each other. Only when two pointers point to the same memory value or are nil are they considered equal. 

The == and != operators can be used to compare pointers in Golang. The equality of most of the pointers can be evaluated; however, certain types have limited support while others have none at all. 

Now we will discuss these two comparing operators that compare the pointer and return the desired result. 

Using == Operator

In the equality operator, if both pointers point to the same variable, this operator returns true. If both pointers link to separate variables, this pointer returns false

Syntax:

Pointer1 == Pointer2 

 

Example:

package main

import "fmt"

func main() {
ptr1 := [2]int{3, 2}
ptr2 := &[2]int{3, 2}
ptr3 := &ptr1
if &ptr1 != ptr2 {
fmt.Println("pointing different")
}
if &ptr1 == ptr3 {
fmt.Println("pointing the same")
}
}

Output:

Remember that a type pointer is not the same as the type itself. Comparing   the two will result in a type mismatch compilation fault.

Using != operator

If both pointers point to the same variable, this operation returns false. If both the pointers link to separate variables, return true.In the given example, &ptr1 is equal to ptr3, but &ptr1 is not equivalent to ptr2

Syntax:

 

Pointer1 != Pointer2 

 

Example:

package main

import "fmt"

func main() {
Num1 := 1234
Num2 := 567

var ptr1 *int
ptr1 = &Num1
ptr2 := &Num2
ptr3 := &Num1

R1 := &ptr1 != &ptr2
fmt.Println("Is ptr1 pointer  = ptr2 pointer: ", R1)

R2 := ptr1 == ptr2
fmt.Println("Is ptr1 pointer = ptr2 pointer: ", R2)

R3 := ptr1 == ptr3
fmt.Println("Is ptr1 pointer = ptr3 pointer: ", R3)

R4 := ptr2 == ptr3
fmt.Println("Is ptr2 pointer = ptr3 pointer: ", R4)

R5 := &ptr3 == &ptr1
fmt.Println("Is ptr3 pointer = ptr1 pointer: ", R5)
}

 

Output :

Frequently Asked Questions

What is Golang used for?

Go (also known as Golang or Go language) is an open-source general-purpose computer language. Google engineers created Go in order to construct stable and efficient software. Go is statically typed and explicit, it is most closely modelled after C language.

Is it possible to compare two pointers in Go?

If pointers point to the same array, we can compare them. Two pointers can be compared using relational pointers that can be either equal to or not equal to operators. Pointers cannot be divided or multiplied.

In Golang, what does a pointer do?

A pointer is a variable in the Go programming language or Golang that stores another variable's  memory address. They are the special variables that are used to store data in the system at a specific memory address.

What are the different sorts of pointers in Go?

In Go, the pointer type is used to point to a location in memory where data is kept. Go uses the * operator to designate a type as a pointer, similar to C/C++. The example below displays many pointers with various underlying types

var ptr1 *int

var ptr2 *float32

var row [ ]*int64

var column [ ]*int32

Var matrix *[1024]int

In Golang, how big is a pointer?

 A pointer is typically the same size as the architecture of your system, 32 bits on a 32-bit system and 64 bits on a 64-bit system.

Conclusion

This blog was meant to introduce you to the concept of comparing pointers in Go.In this blog we discussed the pointers, their different types and their comparison using equal to and not equal to operators with examples. To enhance your understanding you can refer to these articles: Returning Pointer from a Function in GoPointer to an Array as Function Argument in Go and many more

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

       
 

Live masterclass