×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Structures

Go Slices

Go Maps

Go Pointers

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Go Comparing Pointers

Last Updated : July 17, 2025

In Go, pointers can be compared using the equality operators == and !=. This comparison checks whether two pointers point to the same memory address, not whether the values they point to are equal.

Comparing Pointers for Equality

Two pointers are considered equal if they point to the same memory location.

Example

The following example demonstrates how pointer comparisons work in Go by checking if two pointers refer to the same memory address:

package main
import "fmt"

func main() {
    a := 10
    b := 10

    p1 := &a
    p2 := &a
    p3 := &b

    fmt.Println("p1 == p2:", p1 == p2) // true
    fmt.Println("p1 == p3:", p1 == p3) // false
}

When executed, this program outputs:

p1 == p2: true
p1 == p3: false

Even though a and b have the same value, their addresses are different, so p1 == p3 is false.

Comparing Pointers to nil

You can compare a pointer to nil to check whether it has been assigned an address or not.

Example

The following example demonstrates how to check if a pointer is nil in Go before using it:

package main
import "fmt"

func main() {
    var ptr *int

    if ptr == nil {
        fmt.Println("Pointer is nil")
    } else {
        fmt.Println("Pointer is not nil")
    }
}

Output:

Pointer is nil

Pointer Comparison vs Value Comparison

If you want to compare the values pointed to by two pointers, you must dereference them first using the * operator.

Example

The following example demonstrates the difference between comparing pointer addresses and the values they point to:

package main
import "fmt"

func main() {
    x := 42
    y := 42

    px := &x
    py := &y

    fmt.Println("px == py:", px == py)       // false
    fmt.Println("*px == *py:", *px == *py)   // true
}

Output:

px == py: false
*px == *py: true

px and py point to different variables with the same value, so their pointers are not equal but the values are.

Comparing Struct Pointers

Same rules apply when working with pointers to structs—comparison is based on memory address.

Example

The following example demonstrates how pointer equality works with struct instances in Go, showing that even identical data does not imply equal pointer addresses:

package main
import "fmt"

type Student struct {
    name string
}

func main() {
    s1 := Student{name: "Amit"}
    s2 := Student{name: "Amit"}

    p1 := &s1
    p2 := &s1
    p3 := &s2

    fmt.Println("p1 == p2:", p1 == p2) // true
    fmt.Println("p1 == p3:", p1 == p3) // false
}

Output:

p1 == p2: true
p1 == p3: false

Exercise

Choose the correct answers for the following questions about comparing pointers in Go.

  1. What does the == operator do when used between two pointers?
  2. How do you compare the actual values stored at two pointers?
  3. What is the result of comparing an uninitialized pointer with nil?

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.