为什么 mockgen 会在同一个文件中生成所有的接口?

huangapple go评论85阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年7月17日 22:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/73012646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定