BuilderGen
Table of Contents
Introduction #
After working on a few Golang
projects, I found a common issue that I face.
When using large structs, a lot of time was spent just initializing the structs
.
Let us show an example here:
type Person struct{
ID int64
Name string
Email string
PhoneNo string
Friends []*Person
}
When writing unit tests for such cases, the structs will have to be initialized over and over again for each case for the test.
Continuing from the example above, if there were some logic for multiple Person
fields, we will have to write the same attributes over and over again.
After writing way too many of these, I have decided to write a tool to help with that instead.
This is where BuilderGen comes in.
BuilderGen is a code generation tool to generate builder patterns from structs.
Goals of the Project #
The goals of the project is simple.
- Generate
struct
builders automatically using code - Able to support
go:generate
syntax to generate thestructs
.
Installing the Package #
To install the package simple install it using
go install github.com/Jh123x/buildergen@latest
Using the Package #
To use the package, simple use the comment shown below and run the go generate
command.
//go:generate buildergen -src ./test.go -name Person
type Person struct{
...
}
Using the example above, after running go generate
a new PersonBuilder
struct will be created in ./test_builder.go
.
Simply import
the builder to use it.