Doc: English | 中文
A command-line tool to generate constructor code for a struct. It don't need manual installation, just add a comment line to the struct then it works.
It don't need a manual installation. Just add this comment line to the struct you want to generate a constructor.
//go:generate go run github.com/Bin-Huang/[email protected]
For example:
//go:generate go run github.com/Bin-Huang/[email protected]
type UserService struct {
baseService
userRepository *repositories.UserRepository
proRepository *repositories.ProRepository
}
after go generate ./...
, go test
or go build
, you get this:
// constructor_gen.go
// NewUserService Create a new UserService
func NewUserService(baseService baseService, userRepository *repositories.UserRepository, proRepository *repositories.ProRepository) *UserService {
return &UserService{
baseService: baseService,
userRepository: userRepository,
proRepository: proRepository,
}
}
Actually Go will automatically installs it locally, but you can also install manually.
go get -u github.com/Bin-Huang/make-constructor
Now you can use it like this:
//go:generate make-constructor
type UserService struct {
baseService
userRepository *repositories.UserRepository
proRepository *repositories.ProRepository
}
It makes your code easier to write and maintain.
Writing and updating constructor code for many structs can be laborious and error-prone, especially if you have a huge codebase. These should be handed over to automatic tools like this tool.
And it also works well with these dependency injection tool like wire
. That is to say, if you use wire
in your project, you may need this tool very much.
It take care of the generated code.
Don't worry about the imports, variable naming and code style in the generated code.
It dont't need manual installation and other dependency.
It works anywhere there is a GO runtime and network. It don't broke the work of other people who don't have installed this tool in collaboration.
MIT