Skip to content

Commit 19e9c26

Browse files
authored
Always ignore _ fields (#108)
The _ field in Go is special in that it cannot be read from or written to. Thus, skip it by default. It is pointless to require the user to use cmpopts.IgnoreUnexported just for the _ field.
1 parent ba10d0b commit 19e9c26

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

cmp/compare.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ func (s *state) compareStruct(vx, vy reflect.Value, t reflect.Type) {
491491
step.idx = i
492492
step.unexported = !isExported(step.name)
493493
if step.unexported {
494+
if step.name == "_" {
495+
continue
496+
}
494497
// Defer checking of unexported fields until later to give an
495498
// Ignore a chance to ignore the field.
496499
if !vax.IsValid() || !vay.IsValid() {

cmp/compare_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ root[0]["hr"]:
465465
root[1]["hr"]:
466466
-: int(63)
467467
+: float64(63)`,
468+
}, {
469+
label: label,
470+
x: struct{ _ string }{},
471+
y: struct{ _ string }{},
468472
}}
469473
}
470474

0 commit comments

Comments
 (0)