- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Compare Equality of Struct, Slice and Map in Golang?
Golang is a statically typed language that provides developers with a range of built-in data types to work with, including structs, slices, and maps. Comparing the equality of these data types can be a bit tricky, as they have different underlying implementations. In this article, we'll discuss how to compare equality of struct, slice, and map in Golang.
Comparing Equality of Structs in Golang
Structs are composite data types that allow you to group together related values. When comparing two structs in Golang, you need to compare each field of the struct separately. You can do this by using the "==" operator or by using the reflect package's "DeepEqual" function.
Example
Here's an example of how to compare equality of structs using the "==" operator −
package main import ( "fmt" ) type Person struct { Name string Age int } func main() { p1 := Person{Name: "John", Age: 30} p2 := Person{Name: "John", Age: 30} if p1 == p2 { fmt.Println("p1 and p2 are equal") } else { fmt.Println("p1 and p2 are not equal") } }
Output
p1 and p2 are equal
In this example, we create two instances of the Person struct with the same values. We then compare them using the "==" operator. Since both structs have the same values, the comparison will return true.
Comparing Equality of Slices in Golang
Slices are dynamic arrays that can grow or shrink as needed. When comparing two slices in Golang, you need to compare each element of the slice separately. You can do this by using a loop or by using the "reflect.DeepEqual" function.
Example
Here's an example of how to compare equality of slices using a loop −
package main import ( "fmt" ) func main() { s1 := []string{"foo", "bar", "baz"} s2 := []string{"foo", "bar", "baz"} if len(s1) != len(s2) { fmt.Println("s1 and s2 are not equal") return } for i, v := range s1 { if v != s2[i] { fmt.Println("s1 and s2 are not equal") return } } fmt.Println("s1 and s2 are equal") }
Output
s1 and s2 are equal
Comparing Equality of Maps in Golang
Maps are key-value pairs that allow you to store and retrieve values based on a unique key. When comparing two maps in Golang, you need to compare each key-value pair separately. You can do this by using a loop or by using the "reflect.DeepEqual" function.
Example
Here's an example of how to compare equality of maps using a loop −
package main import ( "fmt" ) func main() { m1 := map[string]string{"foo": "bar", "baz": "qux"} m2 := map[string]string{"foo": "bar", "baz": "qux"} if len(m1) != len(m2) { fmt.Println("m1 and m2 are not equal") return } for k, v := range m1 { if m2[k] != v { fmt.Println("m1 and m2 are not equal") return } } fmt.Println("m1 and m2 are equal") }
Output
m1 and m2 are equal
In this example, we create two maps with the same key-value pairs. We then compare them using a loop to iterate over each key-value pair and compare the values. If the lengths or values of the maps differ, the comparison will return false.
Using reflect.DeepEqual()
While using loops to compare structs, slices, and maps is effective, it can be time-consuming and error-prone, especially for more complex data structures. Alternatively, you can use the reflect.DeepEqual() function to compare two values of any type for equality.
Example
Here's an example of how to use reflect.DeepEqual() to compare equality of structs, slices, and maps −
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { p1 := Person{Name: "John", Age: 30} p2 := Person{Name: "John", Age: 30} if reflect.DeepEqual(p1, p2) { fmt.Println("p1 and p2 are equal") } else { fmt.Println("p1 and p2 are not equal") } s1 := []string{"foo", "bar", "baz"} s2 := []string{"foo", "bar", "baz"} if reflect.DeepEqual(s1, s2) { fmt.Println("s1 and s2 are equal") } else { fmt.Println("s1 and s2 are not equal") } m1 := map[string]string{"foo": "bar", "baz": "qux"} m2 := map[string]string{"foo": "bar", "baz": "qux"} if reflect.DeepEqual(m1, m2) { fmt.Println("m1 and m2 are equal") } else { fmt.Println("m1 and m2 are not equal") } }
Output
p1 and p2 are equal s1 and s2 are equal m1 and m2 are equal
In this example, we use reflect.DeepEqual() to compare the equality of structs, slices, and maps. If the values are equal, the comparison will return true. Otherwise, it will return false.
Conclusion
In Golang, comparing the equality of structs, slices, and maps requires comparing each field, element, or key-value pair separately. This can be done using loops or by using the