package cmpopts
Import Path
github.com/google/go-cmp/cmp/cmpopts (on go.dev)
Dependency Relation
imports 11 packages, and imported by one package
Involved Source Files
Package cmpopts provides common options for the cmp package.
ignore.go
sort.go
struct_filter.go
xform.go
Package-Level Type Names (total 11, none are exported)
Package-Level Functions (total 30, in which 14 are exported)
AcyclicTransformer returns a Transformer with a filter applied that ensures
that the transformer cannot be recursively applied upon its own output.
An example use case is a transformer that splits a string by lines:
AcyclicTransformer("SplitLines", func(s string) []string{
return strings.Split(s, "\n")
})
Had this been an unfiltered Transformer instead, this would result in an
infinite cycle converting a string to []string to [][]string and so on.
EquateApprox returns a Comparer option that determines float32 or float64
values to be equal if they are within a relative fraction or absolute margin.
This option is not used when either x or y is NaN or infinite.
The fraction determines that the difference of two values must be within the
smaller fraction of the two values, while the margin determines that the two
values must be within some absolute margin.
To express only a fraction or only a margin, use 0 for the other parameter.
The fraction and margin must be non-negative.
The mathematical expression used is equivalent to:
|x-y| ≤ max(fraction*min(|x|, |y|), margin)
EquateApprox can be used in conjunction with EquateNaNs.
EquateApproxTime returns a Comparer option that determines two non-zero
time.Time values to be equal if they are within some margin of one another.
If both times have a monotonic clock reading, then the monotonic time
difference will be used. The margin must be non-negative.
EquateEmpty returns a Comparer option that determines all maps and slices
with a length of zero to be equal, regardless of whether they are nil.
EquateEmpty can be used in conjunction with SortSlices and SortMaps.
EquateErrors returns a Comparer option that determines errors to be equal
if errors.Is reports them to match. The AnyError error can be used to
match any non-nil error.
EquateNaNs returns a Comparer option that determines float32 and float64
NaN values to be equal.
EquateNaNs can be used in conjunction with EquateApprox.
IgnoreFields returns an Option that ignores fields of the
given names on a single struct type. It respects the names of exported fields
that are forwarded due to struct embedding.
The struct type is specified by passing in a value of that type.
The name may be a dot-delimited string (e.g., "Foo.Bar") to ignore a
specific sub-field that is embedded or nested within the parent struct.
IgnoreInterfaces returns an Option that ignores all values or references of
values assignable to certain interface types. These interfaces are specified
by passing in an anonymous struct with the interface types embedded in it.
For example, to ignore sync.Locker, pass in struct{sync.Locker}{}.
IgnoreMapEntries returns an Option that ignores entries of map[K]V.
The discard function must be of the form "func(T, R) bool" which is used to
ignore map entries of type K and V, where K and V are assignable to T and R.
Entries are ignored if the function reports true.
IgnoreSliceElements returns an Option that ignores elements of []V.
The discard function must be of the form "func(T) bool" which is used to
ignore slice elements of type V, where V is assignable to T.
Elements are ignored if the function reports true.
IgnoreTypes returns an Option that ignores all values assignable to
certain types, which are specified by passing in a value of each type.
IgnoreUnexported returns an Option that only ignores the immediate unexported
fields of a struct, including anonymous fields of unexported types.
In particular, unexported fields within the struct's exported fields
of struct types, including anonymous fields, will not be ignored unless the
type of the field itself is also passed to IgnoreUnexported.
Avoid ignoring unexported fields of a type which you do not control (i.e. a
type from another repository), as changes to the implementation of such types
may change how the comparison behaves. Prefer a custom Comparer instead.
SortMaps returns a Transformer option that flattens map[K]V types to be a
sorted []struct{K, V}. The less function must be of the form
"func(T, T) bool" which is used to sort any map with key K that is
assignable to T.
Flattening the map into a slice has the property that cmp.Equal is able to
use Comparers on K or the K.Equal method if it exists.
The less function must be:
• Deterministic: less(x, y) == less(x, y)
• Irreflexive: !less(x, x)
• Transitive: if !less(x, y) and !less(y, z), then !less(x, z)
• Total: if x != y, then either less(x, y) or less(y, x)
SortMaps can be used in conjunction with EquateEmpty.
SortSlices returns a Transformer option that sorts all []V.
The less function must be of the form "func(T, T) bool" which is used to
sort any slice with element type V that is assignable to T.
The less function must be:
• Deterministic: less(x, y) == less(x, y)
• Irreflexive: !less(x, x)
• Transitive: if !less(x, y) and !less(y, z), then !less(x, z)
The less function does not have to be "total". That is, if !less(x, y) and
!less(y, x) for two elements x and y, their relative order is maintained.
SortSlices can be used in conjunction with EquateEmpty.
Package-Level Variables (only one, which is exported)
AnyError is an error that matches any non-nil error.
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |