# How to compare Structs with the Different Values Assigned to Data Fields in Golang?

When working with Go, it's often necessary to compare structs to determine whether they're equal or not. Comparing two structs can be easy when they have the same values assigned to their data fields. However, comparing structs with different values assigned to their data fields can be a bit more complicated. In this article, we'll discuss how to compare structs with different values assigned to their data fields in Golang.

## Comparing Structs with the Same Values Assigned to Data Fields

Before we dive into comparing structs with different values assigned to their data fields, let's first take a look at how to compare structs with the same values assigned to their data fields. Here's an example −

### Example

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 define a Person struct with two data fields, Name and Age. We then create two instances of this struct, p1 and p2, with the same values assigned to their data fields. Finally, we compare p1 and p2 using the == operator. Since p1 and p2 have the same values assigned to their data fields, the output will be p1 and p2 are equal.

## Comparing Structs with Different Values Assigned to Data Fields

Now let's take a look at how to compare structs with different values assigned to their data fields. Here's an example −

### Example

package main

import (
"fmt"
"reflect"
)

type Person struct {
Name string
Age  int
}

func main() {
p1 := Person{Name: "John", Age: 30}
p2 := Person{Name: "Jane", Age: 25}

if reflect.DeepEqual(p1, p2) {
fmt.Println("p1 and p2 are equal")
} else {
fmt.Println("p1 and p2 are not equal")
}
}


### Output

p1 and p2 are not equal


In this example, we define a Person struct with two data fields, Name and Age. We then create two instances of this struct, p1 and p2, with different values assigned to their data fields. To compare p1 and p2, we use the reflect.DeepEqual() function. This function takes two arguments and returns true if they're deeply equal, and false otherwise. In our case, p1 and p2 are not deeply equal since they have different values assigned to their Name and Age fields, so the output will be p1 and p2 are not equal.

It's important to note that using reflect.DeepEqual() to compare structs can be slow and may not always work as expected. This is because the function performs a deep comparison, which can be expensive and may not handle all cases correctly. A better approach is to write a custom comparison function that compares only the fields that are relevant to the comparison.

## Conclusion

Comparing structs with different values assigned to their data fields in Golang can be a bit tricky, but it's an essential skill for any Go developer. In this article, we covered how to compare structs with the same values assigned to their data fields using the == operator, and how to compare structs with different values assigned to their data fields using the reflect.DeepEqual() function. Remember that using reflect.DeepEqual() can be slow

Updated on: 19-Apr-2023

230 Views