当我不认为自己时,兽医会告诉我,我正在抄袭锁

时间:2017-07-13 14:31:54

标签: pointers go go-toolchain

您可以在https://github.com/bigblind/marvin

找到我正在处理的代码

gometalintergo vet向我发出以下错误:

  

accounts / interactors / accounts_test.go:16 :: error:来自ma的文字副本锁定值:github.com/bigblind/marvin/accounts.MockAccountStore包含github.com/bigblind/marvin/vendor/github.com/stretchr /testify/mock.Mock包含sync.Mutex(vet)

如果您不熟悉gometalinter,那不重要,它只会运行其他工具,包括去兽医。

所以这里是来自accounts/interactors/accounts_test.go:16的代码,其中包含上下文的行:

ma := accounts.NewMockAccountStore()
ma.On("SaveAccount", mock.AnythingOfType("Account")).Return(nil)
ca := CreateAccount{ma}

第16行是上面代码段的最后一行。我们正在考虑accounts.NewMockAccountStore() and putting it in the first field of a new 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结构:

type CreateAccount struct {
    AccountStore domain.AccountStore
}

因此CreateAccount只有一个用于存储domain.AccountStore的字段。 domain.AccountStore是一个MockAccountStore实现的接口。

我能想到的唯一解释是,分配给具有接口类型的字段会以某种方式导致复制,但我不知道为什么,因为据我所知,接口值只是指向基础类型的指针。

有人可以向我解释为什么兽医会抱怨吗?

看起来我实际上并没有复制。

我已按如下方式更改了我的代码:

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)。

那我如何告诉go vet呢?

在@ putu的请求中,这里是accounts/interactors的命令输出:

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

0 个答案:

没有答案