A command-line tool to generate a constructor for the struct.
$ go get -u github.com/moznion/gonstructor/cmd/gonstructor
Also, you can get the pre-built binaries on Releases.
Or get it with gobinaries.com:
curl -sf https://gobinaries.com/moznion/gonstructor | sh
gonstructor depends on goimports for fixing import paths and formatting code, you need to install it:
$ go get golang.org/x/tools/cmd/goimports
Usage of gonstructor:
-constructorTypes string
[optional] comma-separated list of constructor types; it expects "allArgs" and "builder" (default "allArgs")
-init string
[optional] name of function to call on object after creating it
-output string
[optional] output file name (default "srcdir/<type>_gen.go")
-type string
[mandatory] a type name
-version
[optional] show the version information
-withGetter
[optional] generate a constructor along with getter functions for each field
Data encapsulation is a good practice to make software, and it is necessary to clearly indicate the boundary of the structure by controlling the accessibility of the data fields (i.e. private or public) for that. Basically keeping the data fields be private and immutable would be good to make software be robust because it can avoid unexpected field changing.
Golang has a simple way to do that by choosing the initial character's type: upper case or lower case. Once it has decided to use a field as private, it needs to make something like a constructor function, but golang doesn't have a mechanism to support constructor now.
Therefore this project aims to automatically generate constructors to use structures with private and immutable, easily.
- write a struct type with
go:generate
e.g.
//go:generate gonstructor --type=Structure --constructorTypes=allArgs"
type Structure struct {
foo string
bar io.Reader
Buz chan interface{}
}
- execute
go generate ./...
- then
gonstructor
generates a constructor code
e.g.
func NewStructure(foo string, bar io.Reader, buz chan interface{}) *Structure {
return &Structure{foo: foo, bar: bar, Buz: buz}
}
- write a struct type with
go:generate
e.g.
//go:generate gonstructor --type=Structure --constructorTypes=builder"
type Structure struct {
foo string
bar io.Reader
Buz chan interface{}
}
- execute
go generate ./...
- then
gonstructor
generates a buildr code
e.g.
type StructureBuilder struct {
foo string
bar io.Reader
buz chan interface{}
bufferSize int
}
func NewStructureBuilder() *StructureBuilder {
return &StructureBuilder{}
}
func (b *StructureBuilder) Foo(foo string) *StructureBuilder {
b.foo = foo
return b
}
func (b *StructureBuilder) Bar(bar io.Reader) *StructureBuilder {
b.bar = bar
return b
}
func (b *StructureBuilder) Buz(buz chan interface{}) *StructureBuilder {
b.buz = buz
return b
}
func (b *StructureBuilder) BufferSize(bufferSize int) *StructureBuilder {
b.bufferSize = bufferSize
return b
}
func (b *StructureBuilder) Build() *Structure {
return &Structure{
foo: b.foo,
bar: b.bar,
Buz: b.buz,
bufferSize: b.bufferSize,
}
}
- write a struct type with
go:generate
- write a function that initializes internal fields
- pass its name to
-init
parameter
e.g.
//go:generate gonstructor --type=Structure -init construct
type Structure struct {
foo string
bar io.Reader
Buz chan interface{}
bufferSize int
buffer chan []byte `gonstructor:"-"`
}
func (structure *Structure) construct() {
structure.buffer = make(chan []byte, structure.bufferSize)
}
- execute
go generate ./...
- then
gonstructor
generates a buildr code
e.g.
func NewStructure(
foo string,
bar io.Reader,
buz chan interface{},
bufferSize int,
) *Structure {
r := &Structure{
foo: foo,
bar: bar,
Buz: buz,
bufferSize: bufferSize,
}
r.construct()
return r
}
gonstructor:"-"
supports that.
e.g.
type Structure struct {
foo string
bar int64 `gonstructor:"-"`
}
The generated code according to the above structure doesn't contain bar
field.
Binaries are built and uploaded by goreleaser. Please refer to the configuration file: .goreleaser.yml
moznion (moznion@gmail.com)