如何将 https://github.com/go-test/deep 导入到您的 Golang 测试中

发布于 2025-01-16 20:06:32 字数 642 浏览 2 评论 0原文

我是 Golang 新手。

我的 go 版本:1.17.8

尝试编写测试来比较 strcuts。 发现这个库可以显示哪些字段完全等效。 所以想到在 Reflect.DeepEqual 上使用它

,这是我的测试文件,

package main

import (
    "os"
    "testing"

    "github.com/go-test/deep"
)


func TestDeepEqual(t *testing.T) {
    os.Remove("_decktesting.txt")

    d := newDeck()
    d.saveToFile("_decktesting.txt")
    l := newDeckFromFile("_decktesting.txt")

    if diff := deep.Equal(d, l); diff != nil {
        t.Error(diff)
    }
}

这是尝试测试时的内容,

➜ go get

go-training/cards on  develop [!] via 
              

I'm new to Golang.

My go version: 1.17.8

Trying write tests to compare strcuts.
Found this lib can show you which fields exactly equivalent.
So thought of using it over reflect.DeepEqual

Here's my test go file,

package main

import (
    "os"
    "testing"

    "github.com/go-test/deep"
)


func TestDeepEqual(t *testing.T) {
    os.Remove("_decktesting.txt")

    d := newDeck()
    d.saveToFile("_decktesting.txt")
    l := newDeckFromFile("_decktesting.txt")

    if diff := deep.Equal(d, l); diff != nil {
        t.Error(diff)
    }
}

Here's what it says when try to test,

➜ go get

go-training/cards on  develop [!] via ???? v1.17.8 
➜ go test
# go-training/cards
deck_test.go:7:2: cannot find package "github.com/go-test/deep" in any of:
        /usr/local/go/src/github.com/go-test/deep (from $GOROOT)
        /Users/ssamarasin/go/src/github.com/go-test/deep (from $GOPATH)
FAIL    go-training/cards [setup failed]

go-training/cards on  develop [!] via ???? v1.17.8 
➜ go install github.com/go-test/deep@latest
package github.com/go-test/deep is not a main package

Can someone please explain how I can use this lib to compare my structs?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2025-01-23 20:06:32
GO111MODULE=on go get github.com/go-test/deep
GO111MODULE=on go get github.com/go-test/deep
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文