What is Structure Equality?
As the name suggests, Structure equality is the checking of whether two structures are equal or not.
In Go, we are allowed to check whether two or more structures are equal or not. Here structures are equal in the sense that their field values are equal.
So, two structures are equal if their field type are comparable and all the field values are equal.
Does all the field types are comparable in Go?
No, not all the field types are comparable in Go. There are some of the comparable field types are defined by Go specifications are shown below:
- boolean
- numeric
- pointer
- string
- channel
- Interface type
- Array (if all the type value is comparable)
-
Structs (if all its fields are comparable)
Some of the field types that are not comparable are shown below:
Examples
Let’s give some examples,
We can compare the structs using the == operator.
Example1(Using comparable fields)
package main
import "fmt"
// struct A
type A struct {
x int
y string
z int
}
func main() {
a := A{1 , "Hello World" , 2}
b := A{1 , "Hello World" , 2}
c := A{2 , "Hello World" , 10}
if a == b {
fmt.Println("a and b are equal")
}else{
fmt.Println("a and b are not equal")
}
if a == c {
fmt.Println("a and c are equal")
} else {
fmt.Println("a and c are not equal")
}
}
Output

Explanation
- In the above example, we compared structures a, b and c using the “==” operator.
- As a and b are the same, it prints “a and b are equal”.
- As a and c are the same, it prints “a and c are not equal”.
Example2(Using Non-Comparable Fields)
package main
import "fmt"
// struct A
type A struct {
x int
y string
z []string // non comparable field
}
func main() {
a := A{x: 1 , y: "Hello World" , z: []string{"Hello" , "World"}}
b := A{x: 1 , y:"Hello World" , z: []string{"Hello" , "World"}}
// it will raise a compilation error
if a == b {
fmt.Println("a and b are equal")
}else{
fmt.Println("a and b are not equal")
}
}
Output

Explanation
We compared two non-comparable field types, slice in string.slice. It results in a "Compilation error".
Frequently Asked Questions
What is a structure?
A structure is a user-defined type which allows you to combine a group of elements, possibly of different types, into a single type.
What do you mean by structure equality?
In Go, we are allowed to check whether two or more structures are equal or not. Here structures are equal in the sense that their field values are equal. This is known as structure equality.
How do you check for struct equality in Go?
To check for struct equality in Go, we can use the equal to(==) operator. If both are equal, it returns true otherwise returns false.
Does all the field types are comparable in Go?
No, not all the field types are comparable in Go. There are some of the comparable field types are defined by Go specifications are shown below:
- boolean
- numeric
- pointer
- string
- channel
- Interface type
- Array (if all the type value is comparable)
-
Structs (if all its fields are comparable)
Some of the field types that are not comparable are shown below:
Conclusion
In this article, we have extensively discussed the Structure Equality Concept in Go.
We started with the basic introduction, then we discussed,
- About the Structure in Golang
- What is Structure Equality
-
Examples
You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.
Happy Reading!