Quidest?

Unexported fields and cmp.Equal in Go

· Lorenzo Drumond

When you have unexported fields in your struct, but you want to be able to use cmp.Equal in your tests, you’ll run in compilation errors because cmp.Equal cannot see all the fields.

--- FAIL: TestGetAllBooks (0.00s)
panic: cannot handle unexported field at
    {[]bookstore.Book}[1].category: "bookstore".Book
consider using a custom Comparer; if you control the
implementation of type, you can also consider using an
Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
[recovered]

Since it’s quite common in Go to define types with unexported fields, and cmp.Equal is invaluable in tests, you’re bound to run into this problem at some point, so let’s see how to solve it.

Add the following import to your package:

1"github.com/google/go-cmp/cmp/cmpopts"

Modify the calls to cmp.Equal in your tests to change them from this:

1if !cmp.Equal(want, got) {

to this:

1if !cmp.Equal(want, got,
2    cmpopts.IgnoreUnexported(bookstore.Book{})) {

This tells cmp.Equal to ignore any unexported fields on the bookstore.Book struct, preventing the unpleasant panic message.

References

#exported #programming #for_the_love_of_go #values #struct #panic #cmp #golang #variables #methods #validation #equal #unexported