Home >Backend Development >Golang >Why Do Comparisons of Arrays of Pointers to Empty Structs Produce Inconsistent Results?
Why Array Comparison with Empty Structs Yields Different Results
Given an array of pointers to empty structs, why does the equality comparison of the arrays sometimes evaluate to true and sometimes to false?
Exploration of Behavior
package main import "fmt" type myStruct struct{} func main() { s, ss := myStruct{}, myStruct{} arr1 := [6]*myStruct{&s} arr2 := [6]*myStruct{&ss} fmt.Println(&s == &ss, arr1 == arr2) // Produces mixed results (e.g., false, true or true, false) l, ll := myStruct{A: 1}, myStruct{A: 1} arr3 := [6]*myStruct{&l} arr4 := [6]*myStruct{&ll} fmt.Println(&l == &ll, arr3 == arr4) // Always evaluates to false }
Explanation
The Go language specification states that:
Dynamic Behavior and Escape Analysis
The behavior can be explained by considering the escape analysis performed by the Go compiler.
Implications
The above is the detailed content of Why Do Comparisons of Arrays of Pointers to Empty Structs Produce Inconsistent Results?. For more information, please follow other related articles on the PHP Chinese website!