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 ankita_saini Follow Improve Article Tags : Go Language Golang Similar Reads Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google 2 min read Go Programming Language (Introduction) Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env 11 min read time.Sleep() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, t 3 min read Learn Free Programming Languages In this rapidly growing world, programming languages are also rapidly expanding, and it is very hard to determine the exact number of programming languages. Programming languages are an essential part of software development because they create a communication bridge between humans and computers. No 9 min read Golang Tutorial - Learn Go Programming Language This Golang tutorial provides you with all the insights into Go Language programming, Here we provide the basics, from how to install Golang to advanced concepts of Go programming with stable examples. So, if you are a professional and a beginner, this free Golang tutorial is the best place for your 10 min read strings.Contains Function in Golang with Examples strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax:Â func Contains(str, substr string) bool Here, str is the original string and substr is t 2 min read Interfaces in Golang In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t 3 min read Top 10 Golang Project Ideas with Source Code in 2025 Golang, or Go, a programming language was created by Google. It's widely used for building different kinds of applications like websites and cloud services. The fastest way to master this language is by building projects related to it. This article introduces 10 beginner-friendly to medium-difficult 8 min read fmt.Sprintf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. Moreover, this function is defined under the fmt package. Here, you 2 min read Top 10 Golang Frameworks in 2025 Golang (or Go) is an open-source compiled programming language that is used to build simple, systematic, and secure software. It was designed by Google in the year 2007 and has been readily adopted by developers all over the world due to its features like memory safety, structural typing, garbage co 9 min read Like