Home >Backend Development >Golang >The wonderful use of go generation command
The go generation command can automatically generate code to improve development efficiency and code quality: 1. Generate test files (go test -c -o mytestrun your_package); 2. Generate executable files (./mytestrun); 3. Generate mocks Code (mockgen -destination=mocks/notifier.go github.com/yourrepo/yourapp/internal/pkg/notifier Notifier); 4. Generate the structure Marshal/Unmarshal method (go generate ./...).
The go generation command is a powerful tool in the Go language, which can automatically generate code, thereby saving development time and improving Code quality.
go test -c -o mytestrun your_package # 执行test生成可执行文件 ./mytestrun # 运行测试文件
mockgen
is a popular package for generating mock code.
go get -u github.com/golang/mock/mockgen # 安装mockgen包 mockgen -destination=mocks/notifier.go github.com/yourrepo/yourapp/internal/pkg/notifier Notifier # 生成mock代码
go generate ./... # 在当前目录及子目录运行代码生成器 # 生成的代码位于文件 package_stringer.go 中
The go generation command is a powerful tool that can improve development efficiency and code quality by automatically generating code. This article provides several practical examples showing how to use the go generation command to generate test files, mock code, and structure Marshal/Unmarshal methods.
The above is the detailed content of The wonderful use of go generation command. For more information, please follow other related articles on the PHP Chinese website!