Pointers 兽医告诉我我';我正在复制一个锁,但我没有';我想我不是
您可以在以下位置找到我正在编写的代码: 正在从中向我提供以下错误: accounts/interactions/accounts_test.go:16::error:literal从ma:github.com/bigdind/marvin/accounts.MockAccountStore包含github.com/bigdind/marvin/vendor/github.com/stretchr/confidence/mock.mock包含sync.Mutex(vet) 如果你不熟悉gometalinter,那没关系,它只是运行其他工具,包括GoVet 下面是来自Pointers 兽医告诉我我';我正在复制一个锁,但我没有';我想我不是,pointers,go,go-toolchain,Pointers,Go,Go Toolchain,您可以在以下位置找到我正在编写的代码: 正在从中向我提供以下错误: accounts/interactions/accounts_test.go:16::error:literal从ma:github.com/bigdind/marvin/accounts.MockAccountStore包含github.com/bigdind/marvin/vendor/github.com/stretchr/confidence/mock.mock包含sync.Mutex(vet) 如果你不熟悉gometa
accounts/interactions/accounts\u test.go:16
的代码,周围的行表示上下文:
ma := accounts.NewMockAccountStore()
ma.On("SaveAccount", mock.AnythingOfType("Account")).Return(nil)
ca := CreateAccount{ma}
第16行是上面代码段的最后一行。我们将获取accounts.NewMockAccountStore()的结果,并将其放入新的CreateAccount`struct的第一个字段中
以下是MockAccountStore的外观:
type MockAccountStore struct {
*mock.Mock
}
func NewMockAccountStore() *MockAccountStore {
mo := mock.Mock{}
ma := MockAccountStore{&mo}
return &ma
}
正如你在这里看到的,我经常使用指针NewMockAccountStore
返回指向MockAccountStore
的指针,而MockAccountStore
包含指向mock.mock
的指针
因此,当我将这个新创建的MockAccountStore
放在我的CreateAccount
sturct中时,我希望它基本上只将指针分配给该字段,而不复制任何内容
最后,让我们看看CreateAccount
struct:
type CreateAccount struct {
AccountStore domain.AccountStore
}
因此,CreateAccount
只有一个字段用于存储域。AccountStore
domain.AccountStore
是一个接口,由MockAccountStore
实现
我能给出的唯一解释是,分配给具有接口类型的字段会导致复制,但我不确定为什么,因为据我所知,接口值只是指向底层类型的指针
谁能给我解释一下为什么go vet在抱怨
看来我不是在抄袭。
我已将代码更改如下:
ma := accounts.NewMockAccountStore()
ma.On("SaveAccount", mock.AnythingOfType("Account")).Return(nil)
s := fmt.Sprintf("Initial %#v\n", ma)
ca := CreateAccount{ma}
panic(s + fmt.Sprintf("assigned: %#v", ca.AccountStore))
当包含此代码的测试运行时,我收到以下紧急消息:
Initial&accounts.MockAccountStore{Mock:(*Mock.Mock)(0xc420019780)}
已分配:&accounts.MockAccountStore{Mock:(*Mock.Mock)(0xc420019780)}
如您所见,两个值都指向相同的mock.mock
指针(0xc420019780)
那我该如何告诉你去看兽医
应@putu的请求,以下是帐户/交互者的命令输出:
vet: accounts/interactors/accounts_test.go:8:2: could not import github.com/stretchr/testify/require (can't find import: github.com/bigblind/marvin/vendor/github.com/stretchr/testify/require)
Checking file accounts/interactors/accounts.go
Checking file accounts/interactors/login.go
Checking file accounts/interactors/accounts_test.go
accounts/interactors/accounts_test.go:15: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/accounts_test.go:28: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/accounts_test.go:35: github.com/bigblind/marvin/accounts/domain.Account composite literal uses unkeyed fields
accounts/interactors/accounts_test.go:40: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
Checking file accounts/interactors/login_test.go
accounts/interactors/login_test.go:22: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:36: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:47: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:58: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:70: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:80: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
如果MockAccountStore
实现了与*MockAccountStore
相反的接口,则接口值包含指向MockAccountStore
值副本的指针。这可能是问题所在吗?事实上,*MockAccountStore实现了接口。所有接收器都是指针接收器。go-tool-vet-v-All-accounts/interactions
的输出是什么?我正在根据您的描述创建一个测试包,然后运行go vet…
,我没有发现任何错误。我已将命令输出添加到问题中。代码现在已联机: