我很好奇为什么这个DeepEqual检查是错误的:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "reflect"
    "strings"
)

type Result struct {
    Topic string `json:"topic,omitempty"`
    Id    int    `json:"id,omitempty"`
}

// Result represents the returned collection from a topic search.
type ResultResponse struct {
    Result []Result `json:"results"`
}

func main() {
    want := ResultResponse{
        []Result{{Topic: "Clojure", Id: 1000}},
    }

    input := `{"results": [ {"topic": "Clojure", "id": 1000} ]}`
    p := ResultResponse{}

    err := json.NewDecoder(strings.NewReader(input)).Decode(&p)

    if err != nil {
        panic(err)
    }

    fmt.Println(p, want)

    if !reflect.DeepEqual(input, want) {
        log.Printf("returned %+v, want %+v", p, want)
    }


}

最佳答案

这是我的情况:

func TestGoogleAccountRepository_FindByClientCustomerIds(t *testing.T) {
    type args struct {
        ids []int
    }
    tests := []struct {
        name    string
        args    args
        want    []cedar.GoogleAccount
        wantErr bool
    }{
        {
            name:    "should get client customer ids correctly",
            args:    args{ids: []int{9258066191}},
            want:    make([]cedar.GoogleAccount, 0),
            wantErr: false,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := googleAccountRepo.FindByClientCustomerIds(tt.args.ids)
            if (err != nil) != tt.wantErr {
                t.Errorf("GoogleAccountRepository.FindByClientCustomerIds() error = %v, wantErr %v", err, tt.wantErr)
                return
            }

            fmt.Printf("got = %#v, want = %#v\n", got, tt.want)
            if !reflect.DeepEqual(got, tt.want) {
                t.Errorf("GoogleAccountRepository.FindByClientCustomerIds() = %+v, want %+v", got, tt.want)
            }
        })
    }
}

首先运行此测试时,出现以下消息:

load env vars from local fs env file
=== RUN   TestGoogleAccountRepository_FindByClientCustomerIds
--- FAIL: TestGoogleAccountRepository_FindByClientCustomerIds (0.62s)
=== RUN   TestGoogleAccountRepository_FindByClientCustomerIds/should_get_client_customer_ids_correctly
got = []cedar.GoogleAccount(nil), want = []cedar.GoogleAccount{}
    --- FAIL: TestGoogleAccountRepository_FindByClientCustomerIds/should_get_client_customer_ids_correctly (0.62s)
        googleAccount_test.go:64: GoogleAccountRepository.FindByClientCustomerIds() = [], want []
FAIL

请注意以下消息:

GoogleAccountRepository.FindByClientCustomerIds() = [], want []

看来gotwant都是空的slice,对吗?不,在我添加以下代码后:

fmt.Printf("got = %#v, want = %#v\n", got, tt.want)

它打印出:

got = []cedar.GoogleAccount(nil), want = []cedar.GoogleAccount{}

如您所见,got不等于want

这是因为我在googleAccounts方法中声明了googleAccountRepo.FindByClientCustomerIds变量,如下所示:

var googleAccounts []cedar.GoogleAccount

我将其更改为

var googleAccounts = make([]cedar.GoogleAccount, 0)

测试通过:

=== RUN   TestGoogleAccountRepository_FindByClientCustomerIds
--- PASS: TestGoogleAccountRepository_FindByClientCustomerIds (0.46s)
=== RUN   TestGoogleAccountRepository_FindByClientCustomerIds/should_get_client_customer_ids_correctly
got = []cedar.GoogleAccount{}, want = []cedar.GoogleAccount{}
    --- PASS: TestGoogleAccountRepository_FindByClientCustomerIds/should_get_client_customer_ids_correctly (0.46s)
PASS

Process finished with exit code 0

关于go - Golang : reflect. DeepEqual返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29422602/

10-16 00:08