Structure Equality in Golang Last Updated : 02 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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.Example 1: Go // Go program to illustrate the // concept of struct equality // using == operator package main import "fmt" // Creating a structure type Author struct { name string branch string language string Particles int } // Main function func main() { // Creating variables // of Author structure a1 := Author{ name: "Moana", branch: "CSE", language: "Python", Particles: 38, } a2 := Author{ name: "Moana", branch: "CSE", language: "Python", Particles: 38, } a3 := Author{ name: "Dona", branch: "CSE", language: "Python", Particles: 38, } // Checking if a1 is equal // to a2 or not // Using == operator if a1 == a2 { fmt.Println("Variable a1 is equal to variable a2") } else { fmt.Println("Variable a1 is not equal to variable a2") } // Checking if a1 is equal // to a2 or not // Using == operator if a2 == a3 { fmt.Println("Variable a2 is equal to variable a3") } else { fmt.Println("Variable a2 is not equal to variable a3") } } Output: Variable a1 is equal to variable a2 Variable a2 is not equal to variable a3 Example 2: Go // Go program to illustrate the // concept of struct equality // using DeepEqual() method package main import ( "fmt" "reflect" ) // Creating a structure type Author struct { name string branch string language string Particles int } // Main function func main() { // Creating variables // of Author structure a1 := Author{ name: "Soana", branch: "CSE", language: "Perl", Particles: 48, } a2 := Author{ name: "Soana", branch: "CSE", language: "Perl", Particles: 48, } a3 := Author{ name: "Dia", branch: "CSE", language: "Perl", Particles: 48, } // Comparing a1 with a2 // Using DeepEqual() method fmt.Println("Is a1 equal to a2: ", reflect.DeepEqual(a1, a2)) // Comparing a2 with a3 // Using DeepEqual() method fmt.Println("Is a2 equal to a3: ", reflect.DeepEqual(a2, a3)) } Output: Is a1 equal to a2: true Is a2 equal to a3: false Comment More infoAdvertise with us Next Article Embedded Structures in Golang A ankita_saini Follow Improve Article Tags : Go Language Golang Similar Reads Structures in Golang A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 7 min read Structures in Golang A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 7 min read Structures in Golang A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 7 min read Embedded Structures in Golang In the Go programming language, Embedded Structures are a way to reuse a struct's fields without having to inherit from it. It allows you to include the fields and methods of an existing struct into a new struct. The new struct can add additional fields and methods to those it inherits. Syntax: type 2 min read Nested Structure in Golang 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. Go language allows nested structure. A structure which is the 3 min read Nested Structure in Golang 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. Go language allows nested structure. A structure which is the 3 min read Like