google / google/go-cmp

unhelpful output for difference in slices

Open
#350 1 comment 0 reactions 0 assignees View on GitHub
reporter
Dominant language
Go
Stars
4.7k
Forks
243
PR merge metrics
No merged PRs in 30d

Description

In the code below, `Diff` shows the following "differences" between two data structures:

```
main.Array{
main.Integer(0),
- s"",
+ s"",
}
```

Since the `+` and `-` lines are the same, this is not very helpful. It would be nice if `Diff` would show the actual differences. Here is the code (also on the [Go Playground](https://go.dev/play/p/_w_rOBugY4H)):

```
package main

import (
"fmt"
"strconv"
"strings"

"github.com/google/go-cmp/cmp"
)

type Integer int64

type Array []interface{}

func (x Array) String() string {
res := []string{}
res = append(res, "Array")
res = append(res, strconv.FormatInt(int64(len(x)), 10)+" elements")
return "<" + strings.Join(res, ", ") + ">"
}

func main() {
a := Array{Integer(0), Array{float64(1), float64(2), float64(3)}}
b := Array{Integer(0), Array{Integer(1), Integer(2), Integer(3)}}
fmt.Println(cmp.Diff(a, b))
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.