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

huangapple go评论67阅读模式
英文:

How to Import https://github.com/go-test/deep into Your Golang Test

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Golang还不熟悉。

我的go版本是1.17.8。

我正在尝试编写测试来比较结构体。
我找到了一个名为这个的库,可以显示出哪些字段是完全相等的。
所以我想使用它来替代reflect.DeepEqual。

这是我的测试go文件:

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 🐹 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

有人可以解释一下我如何使用这个库来比较我的结构体吗?

谢谢

英文:

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

答案1

得分: 1

GO111MODULE=on go get github.com/go-test/deep

英文:
GO111MODULE=on go get github.com/go-test/deep

huangapple
  • 本文由 发表于 2022年3月25日 12:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/71612233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定