以下是我的文件夹 struct :

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
)

这是我的界面.开始

package interfaces

type MyInterface interface {
    DoSomething() error
    GetValue() int
}

当我try 运行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

如何正确使用moockgen命令?

推荐答案

你可能应该使用Uber的维护分支(因为Google放弃了Golang/mock):https://github.com/uber-go/mock

如果只运行mockgen命令,就会得到命令选项.应该对你起作用的东西是这样的:

mockgen -source interfaces/interfaces.go -destination mocks/mocks.go

Go相关问答推荐

Go 1.22 net/http群组路由

使用恶意软件 scanner (ClamAV)时的Google云存储桶上传文件验证

Golang ==错误:OCI运行时创建失败:无法启动容器进程:exec:./" bin:stat./" bin:没有这样的文件或目录:未知

Kafka消费者在需要时不会暂停

困扰围棋官方巡回赛的S建议所有方法都使用同一类型的接收器

GoLang:无法发送带有附件的邮箱

错误.如果它包含切片,则返回FALSE

重新赋值变量时未清除动态类型-这是错误吗?

如何模拟 stripe 需要 webhooks 的捕获事件?

Go Gin:验证 base64

转到 bufio.Writer、gzip.Writer 并上传到内存中的 AWS S3

用于提取 <*n 的正则表达式(其中 n 是一个数字)

如何以干净的方式在中间件中注入 repo 或服务?

使用 golang 生成 vim

将shell输出绑定到Go中的 struct 的最佳方法?

如何在 GORM 中迭代一个 int 数组

通用函数与外部包中的常见成员一起处理不同的 struct ?

如何在测试中传递用户名和密码等参数

golangci-lint 的 GitHub 操作失败,无法加载 fmt

Beego - 我需要context.Context而不是 Beego 上下文