0
0
GoHow-ToBeginner · 3 min read

How to Compare Structs in Go: Simple Guide with Examples

In Go, you can compare structs directly using the == operator if all their fields are comparable. For structs with non-comparable fields, use reflect.DeepEqual to check equality.
📐

Syntax

You can compare two structs using the == operator if all their fields are comparable types like numbers, strings, or other comparable structs. The syntax is:

struct1 == struct2

If the structs contain fields like slices, maps, or functions, which are not comparable, the == operator cannot be used. Instead, use reflect.DeepEqual(struct1, struct2) to compare deeply.

go
type Person struct {
    Name string
    Age  int
}

var p1, p2 Person

// Compare using ==
if p1 == p2 {
    // structs are equal
}
💻

Example

This example shows comparing two structs with comparable fields using == and two structs with a slice field using reflect.DeepEqual.

go
package main

import (
    "fmt"
    "reflect"
)

type Person struct {
    Name string
    Age  int
}

type Team struct {
    Name    string
    Members []string
}

func main() {
    p1 := Person{Name: "Alice", Age: 30}
    p2 := Person{Name: "Alice", Age: 30}
    p3 := Person{Name: "Bob", Age: 25}

    fmt.Println("p1 == p2:", p1 == p2) // true
    fmt.Println("p1 == p3:", p1 == p3) // false

    t1 := Team{Name: "Dev", Members: []string{"Alice", "Bob"}}
    t2 := Team{Name: "Dev", Members: []string{"Alice", "Bob"}}
    t3 := Team{Name: "Dev", Members: []string{"Bob", "Alice"}}

    fmt.Println("t1 == t2:", reflect.DeepEqual(t1, t2)) // true
    fmt.Println("t1 == t3:", reflect.DeepEqual(t1, t3)) // false
}
Output
p1 == p2: true p1 == p3: false t1 == t2: true t1 == t3: false
⚠️

Common Pitfalls

Trying to use == on structs with non-comparable fields like slices or maps causes a compile error. Also, reflect.DeepEqual considers order and types strictly, so two slices with same elements in different order are not equal.

Example of wrong and right ways:

go
package main

import (
    "fmt"
    "reflect"
)

type Data struct {
    Values []int
}

func main() {
    d1 := Data{Values: []int{1, 2, 3}}
    d2 := Data{Values: []int{1, 2, 3}}

    // Wrong: compile error
    // fmt.Println(d1 == d2)

    // Right: use reflect.DeepEqual
    fmt.Println(reflect.DeepEqual(d1, d2)) // true
}
Output
true
📊

Quick Reference

  • Use == for structs with only comparable fields.
  • Use reflect.DeepEqual for structs with slices, maps, or other non-comparable fields.
  • Remember that reflect.DeepEqual checks order and types strictly.
  • Compile errors occur if you try == on structs with non-comparable fields.

Key Takeaways

Use the == operator to compare structs if all fields are comparable.
Use reflect.DeepEqual to compare structs with slices, maps, or other non-comparable fields.
reflect.DeepEqual checks deeply but is strict about order and types.
Trying == on structs with non-comparable fields causes compile errors.
Choose the comparison method based on the struct's field types.