When Does Go’s == Operator Work? A Deep Dive into Struct, Slice, and Map Comparisons
This article explores the various ways to compare structs, slices, maps, and other Go types, explaining when the == operator is applicable, why it fails for certain types, and how alternatives like reflect.DeepEqual, cmp.Equal, and testify can be used effectively.
Comparing two struct, map, or slice values for equality is a common need in Go development. The article reviews the built‑in == operator, its applicable types, and why it cannot be used with slices, maps, or nil values.
== Comparison
Go’s basic types (e.g., int, string) support ==. Composite types such as struct can use == only if all fields are comparable; fields that are slices or maps make the struct incomparable. Slices and maps can only be compared to nil, and comparing two nils with == is invalid.
Channel Comparison
Channels are reference types; they can be compared with == as long as the channel types match, because the comparison checks the underlying address.
Struct Comparison
Simple structs containing only comparable fields (value types or pointers) can be compared with ==. Structs that embed slices or maps cannot. Example:
type Value struct {
Name string
Gender string
}
func main() {
v1 := Value{Name: "test", Gender: "男"}
v2 := Value{Name: "test", Gender: "男"}
if v1 == v2 { fmt.Println("true") }
}When a struct contains pointer fields, equality depends on whether the pointers reference the same address.
reflect.DeepEqual and cmp.Equal
reflect.DeepEqual()
The reflect package provides a deep, recursive comparison that works for slices, maps, structs, and functions. It considers two values equal if they have the same type and all corresponding elements or fields are deeply equal.
if reflect.DeepEqual(s1, s2) { fmt.Println("struct true") }
mp1 := map[int]int{1:10, 2:20}
mp2 := map[int]int{1:10, 2:20}
if reflect.DeepEqual(mp1, mp2) { fmt.Println("maps equal") }cmp.Equal()
The Google‑maintained go-cmp library offers a safer, more configurable alternative to reflect.DeepEqual. It allows ignoring unexported fields, applying custom transformers, and handling complex comparison logic.
func Equal(x, y interface{}, opts ...Option) bool {
s := newState(opts)
s.compareAny(rootStep(x, y))
return s.result.Equal()
}Differences
cmp.Equaldoes not compare unexported fields by default (it panics), whereas reflect.DeepEqual does. cmp provides richer options for ignoring fields, tolerating differences, and customizing comparison behavior.
Other Comparison Methods
testify’s assert.Equal()
In unit tests, assert.Equal() uses bytes.Equal for []byte and falls back to reflect.DeepEqual for other types.
bytes.Equal()
The standard library’s bytes.Equal (or bytes.Compare) is the preferred way to compare byte slices efficiently.
Performance and Summary
Efficiency Comparison
Simple == comparisons are fastest. Custom comparison functions for known structures are next. For complex or unknown structures, cmp.Equal or reflect.DeepEqual are convenient but slower. assert.Equal ultimately relies on reflect.DeepEqual.
Conclusion
There are many ways to determine equality of Go values, each with trade‑offs in safety and performance. cmp offers the most flexibility and safety, while == remains the quickest for comparable types.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
