A Go factory method generator. Parses all packages to find all struct
signatures. Then generates a fm_gen.go
file for each package.
// Sample demo struct
type Sample struct {
ID int64 `fmgen:"-"`
Name string
Age int64 `fmgen:"optional"`
LastUpdated time.Time
}
// NewSample generated factory method for Sample
func NewSample(Name string, LastUpdated time.Time, Age *int64) *Sample {
result := &Sample{
Name: Name,
LastUpdated: LastUpdated,
}
if Age != nil {
result.Age = *Age
}
return result
}
-d
will specify the directory to parse (defaults to ./)
-r
recursively process directories (defaults to true)
-f
overrides -d
, will process a single file instead
-s
comma separated list of structs to include. all others will be ignored
-v
verbose mode, will include additional logging
This will search the directory recursively and only process Struct1
fmgen -d ./pkg -s Struct1
Adding fmgen:-
to a struct comment will exclude that struct from generation
// Sample demo struct, fmgen:-
type Sample struct {
...
}
Adding fmgen:-
to a struct field will omit that field
type Sample struct {
ID int64 `fmgen:"-"`
...
}
Adding fmgen:optional
to a struct field will allow a nil
to be passed in. A nil
will be skipped when creating the struct
type Sample struct {
ID int64 `fmgen:"optional"`
...
}