gomock学习

https://studygolang.com/articles/10124

安装

go get github.com/golang/mock/gomock

编译mockgen

cd $GOPATH/src/github.com/golang/mock/mockgen
go build

会在$GOPATH/src/github.com/golang/mock/mockgen下生成一个mocken可执行程序,Windows为mkcogen.exe,copy这个mockgen.exe到$GOPATH/bin

mv mockgen $GOPATH/bin

验证mockgen是否安装成功

mockgen

gomock学习_第1张图片

出现以上信息说明安装成功。如果出现 

-bash: mockgen: command not found

则表示安装失败,检查go path设置是否正确。

 

获取gomock文档

go doc github.com/golang/mock/gomock

gomock在线文档

https://godoc.org/github.com/golang/mock/gomock

mockgen指令参数

https://github.com/golang/mock#running-mockgen

-source :A file containing interfaces to be mocked.

-destination :A file to which to write the resulting source code. If you don't set this, the code is printed to standard output.

-package :The package to use for the resulting mock class source code. If you don't set this, the package name is mock_ concatenated with the package of the input file.

-imports :A list of explicit imports that should be used in the resulting source code, specified as a comma-separated list of elements of the form foo=bar/baz, where bar/baz is the package being imported and foo is the identifier to use for the package in the generated source code.

-aux_files: A list of additional files that should be consulted to resolve e.g. embedded interfaces defined in a different file. This is specified as a comma-separated list of elements of the form foo=bar/baz.go, where bar/baz.go is the source file and foo is the package name of that file used by the -source file.

-build_flags: (reflect mode only) Flags passed verbatim to go build.

-mock_names: A list of custom names for generated mocks. This is specified as a comma-separated list of elements of the form Repository=MockSensorRepository,Endpoint=MockSensorEndpoint, where Repository is the interface name and MockSensorRepository is the desired mock name (mock factory method and mock recorder will be named after the mock). If one of the interfaces has no custom name specified, then default naming convention will be used.


-copyright_file: Copyright file used to add copyright header to the resulting source code.

 

 

 

你可能感兴趣的:(go,后台)