A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. This concept is generally compared with the classes in object-oriented programming. It can be termed as a lightweight class which does not support inheritance but supports composition.
In Go language, you are allowed to compare two structures if they are of the same type and contain the same fields values with the help of == operator or DeeplyEqual() Method. Both the operator and method return true if the structures are identically equal(in terms of their fields values) to each other, otherwise, return false. And, if the compared variables belong to different structures, then the compiler will give an error. Let us discuss this concept with the help of the examples:
Note: The DeeplyEqual() method is defined under “reflect” package.
Variable a1 is equal to variable a2 Variable a2 is not equal to variable a3
Is a1 equal to a2: true Is a2 equal to a3: false
- How to check equality of slices of bytes in Golang?
- How to check String for equality under Unicode case folding in Golang?
- Checking Slice of bytes for equality under Unicode case folding in Golang
- How to Compare Equality of Struct, Slice and Map in Golang?
- Anonymous Structure and Field in Golang
- Nested Structure in Golang
- Function as a Field in Golang Structure
- Promoted Fields in Golang Structure
- Promoted Methods in Golang Structure
- Checking if structure is empty or not in Golang
- Printing structure variables in console in Golang
- 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
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.