Skip to content

Unexpected behavior when comparing slices of maps #383

@Yi-Liang-Leon

Description

@Yi-Liang-Leon

version: go-cmp@v0.7.0, go@v1.21.3
When I was comparing slices of maps defined as:

a := []any{map[string]any{"a": 1, "b": 2, "c": 3}}
b := []any{map[string]any{"a": 2, "b": 4, "c": 3}}
fmt.Println(cmp.Diff(a,b))

The result is:

[]any{
    map[string]any{
-       "a": int(1)
-       "a": int(2)
-       "b": int(2)
-       "b": int(4)
        "c": int(3)
    },
}

Which means it traversed inside the map entries.

However, when I was doing this:

a := []any{map[string]any{"a": 1, "b": 2, "c": 3}}
b := []any{map[string]any{"a": 2, "b": 4, "c": 6}}
fmt.Println(cmp.Diff(a,b))

The result become:

[]any{
-    map[string]any{"a": int(1), "b": int(2), "c": int(3)},
+    map[string]any{"a": int(2), "b": int(4), "c": int(6)},
}

I've tested several tests, and it seems that it stops traversing only when the map have three or more differences. Is it a bug, or am I doing anything wrong? Since I'm using go-cmp to handle deserialized objects, this case will affect the path filter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions