Home >Backend Development >Golang >mockgen does not create mocks
This is my folder structure:
demo/ |-- go.mod |-- main.go |-- interfaces/ | |-- interfaces.go |-- mocks/
This is my go.mod file
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 )
This is my interfaces.go
package interfaces type MyInterface interface { DoSomething() error GetValue() int }
When I try to run the mockgen command, instead of creating the mock, it gives me this help description: mockgen has two operating modes: source and reflect. Source mode generates mock interfaces from source files. It is enabled by using the -source flag. Other flags that may be useful in this mode are -imports and -aux_files.
https://manpages.org/mockgen
I ran the following command: mockgen -destination=mocks/mock_myinterface.go -package=mocks demo/interfaces MyInterface
How to use mockgen command correctly?
You should probably use the fork maintained by Uber (because Google abandoned golang/mock): https: //github.com/uber -go/mock
If you just run the mockgen
command you will get the command options. Something that should work for you is this:
mockgen -source interfaces/interfaces.go -destination mocks/mocks.go
The above is the detailed content of mockgen does not create mocks. For more information, please follow other related articles on the PHP Chinese website!