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
Recommended Posts:
- Comparing Maps in Golang
- Pointers in Golang
- Pointers to a Function in Go
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- Import in GoLang
- time.Round() Function in Golang With Examples
- How to add a method to struct type in Golang?
- Converting a string variable into Boolean, Integer or Float type in Golang
- Check if the given slice is sorted in Golang
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.

