Optional function generator from struct.
- Flag
-wcurrently cannot update generated code, only append code
go install github.com/lumochift/optgen@latest
- Help function
Usage of ./optgen:
-all
generate all fields
-file string
path file
-name string
struct name
-tag string
custom tag (default "opt")
-w enable write mode- Generate file with default tags
opt
//thing.go package foo type Thing struct { Field1 string `opt` Field2 []*int `opt` Field3 map[byte]float64 }
For example we have thing.go and need to generate functional option for struct Thing, only field with opt will generated:
optgen -file thing.go -name Thing
Output:
// NewThing returns a *Thing. func NewThing(opt ...func(*Thing)) *Thing { // Prepare a Thing thing := &Thing{} // Apply options. for _, o := range opt { o(thing) } // Do anything here return thing } // SetField1 sets the Field1 func SetField1(field1 string) func(*Thing) { return func(c *Thing) { c.Field1 = field1 } } // SetField2 sets the Field2 func SetField2(field2 []*int) func(*Thing) { return func(c *Thing) { c.Field2 = field2 } }