Skip to content

Commit

Permalink
Always ignore _ fields (#108)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsnet committed Feb 17, 2019
1 parent ba10d0b commit 19e9c26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmp/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ func (s *state) compareStruct(vx, vy reflect.Value, t reflect.Type) {
step.idx = i
step.unexported = !isExported(step.name)
if step.unexported {
if step.name == "_" {
continue
}
// Defer checking of unexported fields until later to give an
// Ignore a chance to ignore the field.
if !vax.IsValid() || !vay.IsValid() {
Expand Down
4 changes: 4 additions & 0 deletions cmp/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ root[0]["hr"]:
root[1]["hr"]:
-: int(63)
+: float64(63)`,
}, {
label: label,
x: struct{ _ string }{},
y: struct{ _ string }{},
}}
}

Expand Down

0 comments on commit 19e9c26

Please sign in to comment.