英文:
Why mockgen generates all interfaces in file?
问题
我有一个带有接口的包
package worker
import "context"
//go:generate mockgen -source interfaces.go -destination mock-interfaces.go -package worker Doer
type (
Doer interface {
Do(ctx context.Context) error
}
LazyThing interface {
Rest(ctx context.Context) error
}
)
我以为只会为Doer
接口生成模拟类。但是我总是得到两个接口的模拟类。
我该如何限制处理的接口列表?
英文:
I have a package with interfaces
package worker
import "context"
//go:generate mockgen -source interfaces.go -destination mock-interfaces.go -package worker Doer
type (
Doer interface {
Do(ctx context.Context) error
}
LazyThing interface {
Rest(ctx context.Context) error
}
)
I assumed that mock class will be generated for Doer
interface only. But I always get mocks for both.
How can I limit the list of processed interfaces?
答案1
得分: 1
当在命令中使用-source interfaces.go
时,您正在启用生成器的源模式。
您需要提供一个表单,从完全限定的包名中导入符号Doer
。
类似于:
//go:generate mockgen -package worker -destination mock-interfaces.go github.com/yourhandle/worker Doer
然后它应该只从包中选择该符号,而不是全部选择。
英文:
When using -source interfaces.go
in the command, you are enabling the source mode of the generator.
You need to provide the form which imports the symbol Doer
from the fully-qualified package name
Something like:
//go:generate mockgen -package worker -destination mock-interfaces.go github.com/yourhandle/worker Doer
Then it should pick only that symbol from the package and not all of them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论