Getters is a code generator tool for Go. It generates getter methods for structs with pointer fields.
Getters is a standalone version of gen-accessors
from
google/go-github. Aside from it's
standalone, it's customizable too and has more primitive types support such as
floats and uints.
type Pony struct {
Name *string
Age *int64
}
Generates...
func (p *Pony) GetAge() int64 {
if p == nil || p.Age == nil {
return 0
}
return *p.Age
}
func (p *Pony) GetName() string {
if p == nil || p.Name == nil {
return ""
}
return *p.Name
}
go get -u github.com/tors/getters
# Basic usage
# Looks for .getters.yml. If it doesn't exist, this will generate
# getter methods for all struct pointer fields.
getters
# Checks custom_getters.yml with verbose mode on
getters -v custom_getters.yml
# Merge and override options
getters -v -s Pony,Kitten -m Horse.GetName,Cat.GetAge
# Ignore main package
getters -p main
//go:generate getters
//go:generate getters -v -s Pony,Kitten -m Horse.GetName,Cat.GetAge
Here's an example of a config file