Thursday, July 10, 2025
HomeLanguagesGolangComparing Pointers in Golang

Comparing Pointers in Golang

Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.).
In Go language, you are allowed to compare two pointers with each other. Two pointers values are only equal when they point to the same value in the memory or if they are nil. You can perform a comparison on pointers with the help of == and != operators provided by the Go language:

1. == operator: This operator return true if both the pointer points to the same variable. Or return false if both the pointer points to different variables.

Syntax:

pointer_1 == pointer_2

Example:




// Go program to illustrate the
// concept of comparing two pointers
package main
  
import "fmt"
  
func main() {
  
    val1 := 2345
    val2 := 567
  
    // Creating and initializing pointers
    var p1 *int
    p1 = &val1
    p2 := &val2
    p3 := &val1
  
    // Comparing pointers 
    // with each other
    // Using == operator
    res1 := &p1 == &p2
    fmt.Println("Is p1 pointer is equal to p2 pointer: ", res1)
  
    res2 := p1 == p2
    fmt.Println("Is p1 pointer is equal to p2 pointer: ", res2)
  
    res3 := p1 == p3
    fmt.Println("Is p1 pointer is equal to p3 pointer: ", res3)
  
    res4 := p2 == p3
    fmt.Println("Is p2 pointer is equal to p3 pointer: ", res4)
  
    res5 := &p3 == &p1
    fmt.Println("Is p3 pointer is equal to p1 pointer: ", res5)
}


Output:

Is p1 pointer is equal to p2 pointer:  false
Is p1 pointer is equal to p2 pointer:  false
Is p1 pointer is equal to p3 pointer:  true
Is p2 pointer is equal to p3 pointer:  false
Is p3 pointer is equal to p1 pointer:  false

 

2. != operator: This operator return false if both the pointer points to the same variable. Or return true if both the pointer points to different variables.

Syntax:

pointer_1 != pointer_2

Example:




// Go program to illustrate the 
// concept of comparing two pointers
package main
  
import "fmt"
  
func main() {
  
    val1 := 2345
    val2 := 567
  
    // Creating and initializing pointers
    var p1 *int
    p1 = &val1
    p2 := &val2
    p3 := &val1
  
    // Comparing pointers
    // with each other
    // Using != operator
    res1 := &p1 != &p2
    fmt.Println("Is p1 pointer not equal to p2 pointer: ", res1)
  
    res2 := p1 != p2
    fmt.Println("Is p1 pointer not equal to p2 pointer: ", res2)
  
    res3 := p1 != p3
    fmt.Println("Is p1 pointer not equal to p3 pointer: ", res3)
  
    res4 := p2 != p3
    fmt.Println("Is p2 pointer not equal to p3 pointer: ", res4)
  
    res5 := &p3 != &p1
    fmt.Println("Is p3 pointer not equal to p1 pointer: ", res5)
}


Output:

Is p1 pointer not equal to p2 pointer:  true
Is p1 pointer not equal to p2 pointer:  true
Is p1 pointer not equal to p3 pointer:  false
Is p2 pointer not equal to p3 pointer:  true
Is p3 pointer not equal to p1 pointer:  true
RELATED ARTICLES

Most Popular

Dominic
32126 POSTS0 COMMENTS
Milvus
66 POSTS0 COMMENTS
Nango Kala
6510 POSTS0 COMMENTS
Nicole Veronica
11658 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11714 POSTS0 COMMENTS
Shaida Kate Naidoo
6605 POSTS0 COMMENTS
Ted Musemwa
6865 POSTS0 COMMENTS
Thapelo Manthata
6565 POSTS0 COMMENTS
Umr Jansen
6558 POSTS0 COMMENTS