aboutsummaryrefslogtreecommitdiff
path: root/cmp
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2017-07-14 17:49:13 -0600
committerGitHub <noreply@github.com>2017-07-14 17:49:13 -0600
commite6ff442c1751c5257df71dd2a42b9bcbbd6f01a6 (patch)
treee6f603665ac5cf8f91c76e44338c5843b7175f3b /cmp
parente6ad8e8c6f4959eb2ffe240ab826c76b2fb855fd (diff)
downloadgo-cmp-e6ff442c1751c5257df71dd2a42b9bcbbd6f01a6.tar.gz
Add helpful suggestions to panic message (#10)
Users find it unhelpful to simply panic saying that unexported fields cannot be handled. Augment the panic message by pointing the user to functions that can be used to address the issue.
Diffstat (limited to 'cmp')
-rw-r--r--cmp/compare.go5
1 files changed, 3 insertions, 2 deletions
diff --git a/cmp/compare.go b/cmp/compare.go
index 455ecb8..2f40753 100644
--- a/cmp/compare.go
+++ b/cmp/compare.go
@@ -67,7 +67,7 @@ import (
//
// Structs are equal if all of their fields are equal. If a struct contains
// unexported fields, Equal panics unless the AllowUnexported option is used or
-// an Ignore option ignores that field.
+// an Ignore option (e.g., cmpopts.IgnoreUnexported) ignores that field.
// Slices and arrays are equal if they have the same length and the elements
// at each index are equal.
// Maps are equal if their keys are exactly equal (according to the == operator)
@@ -279,7 +279,8 @@ func (s *state) tryOptions(vx, vy *reflect.Value, t reflect.Type) bool {
// are either exported or can be forcibly exported.
if sf, ok := s.curPath[len(s.curPath)-1].(*structField); ok && sf.unexported {
if !sf.force {
- panic(fmt.Sprintf("cannot handle unexported field: %#v", s.curPath))
+ const help = "consider using AllowUnexported or cmpopts.IgnoreUnexported"
+ panic(fmt.Sprintf("cannot handle unexported field: %#v\n%s", s.curPath, help))
}
// Use unsafe pointer arithmetic to get read-write access to an