Open TianZhH opened 4 years ago
This is my code:
package main import ( "encoding/json" "fmt" diff "github.com/yudai/gojsondiff" "github.com/yudai/gojsondiff/formatter" ) func main() { textLeft := `{ "name": "text_left", "strategy_list": [ { "percent": 100, "whitelist": [ "aabb" ], "name": "策略1", "stra_id": 11 } ] }` textRight := `{ "business_id": 3, "name": "text_right", "desc": "text_right desc", "strategy_list": [ { "percent": 50, "whitelist": null, "name": "stra_1", "stra_id": 1 }, { "percent": 50, "whitelist": null, "name": "stra_2", "stra_id": 2 } ] }` // textLeft, textRight = textRight, textLeft differ := diff.New() d, err := differ.Compare([]byte(textLeft), []byte(textRight)) if err != nil { return } config := formatter.AsciiFormatterConfig{ShowArrayIndex: true, Coloring: true} var aJson map[string]interface{} _ = json.Unmarshal([]byte(textLeft), &aJson) f := formatter.NewAsciiFormatter(aJson, config) result, err := f.Format(d) if err != nil { return } println(fmt.Sprintf("result=%s", result)) }
The result is like this
But when I swapped textLeft and textRight, the result became like this
// swap textLeft and textRight before diff.New() textLeft, textRight = textRight, textLeft
I think the second result is crorrect. The first result is missing the original content of leftText
This is my code:
The result is like this
But when I swapped textLeft and textRight, the result became like this
I think the second result is crorrect. The first result is missing the original content of leftText