
这是我的文件夹结构:
demo/ |-- go.mod |-- main.go |-- interfaces/ | |-- interfaces.go |-- mocks/
这是我的 go.mod 文件
go 1.20
require (
github.com/golang/mock v1.6.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
这是我的interfaces.go
package interfaces
type MyInterface interface {
DoSomething() error
GetValue() int
}
当我尝试运行mockgen命令时,它并没有创建模拟,而是给了我这个帮助描述: mockgen 有两种操作模式:source 和reflect。Source 模式从源文件生成mock 接口。它是通过使用 -source 标志启用的。在此模式下可能有用的其他标志是 -imports 和 -aux_files。
https://manpages.org/mockgen
我运行了以下命令:mockgen -destination=mocks/mock_myinterface.go -package=mocks demo/interfaces MyInterface
ZipMarket程序仿自Envato旗下网站,对于想创建数字内容/素材交易平台的站长来说,ZipMarket是一个十分独特和极具创新的解决方案,用户在你的网站注册并购买或出售数字内容/素材作品时,你可以获得佣金;用户推广用户到你的网站购买或出售数字内容/素材时,引入用户的用户也可以获得佣金。实际上,ZipMarket是一套完美的数字内容类自由职业生态系统,功能不仅限于素材交易,除了模板/主题、文
如何正确使用mockgen命令?
正确答案
您可能应该使用 Uber 维护的分支(因为 Google 放弃了 golang/mock):https: //github.com/uber-go/mock
如果您只是运行 mockgen 命令,您将获得命令选项。应该对您有用的东西是这样的:
mockgen -source interfaces/interfaces.go -destination mocks/mocks.go









