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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.