go-optioner
is a tool for generating functional options pattern in Go code. This tool can automatically generate corresponding options code based on the given struct definition.
English | 中文简体
- 1、
go install github.com/chenmingyong0423/go-optioner/cmd/optioner@latest
- 2、Run the
optioner
command to check if the installation is successful.
> optioner
optioner is a tool for generating functional options pattern.
Usage:
optioner [flags]
Flags:
-type <struct name>
-output <output path>, default: srcDir/opt_xxx_gen.go
-with_prefix <the prefix of the With{filed_name} function>, default is With{filed_name}.If specified, such as User, it will generate WithUser{filed_name}
-mode <the file writing mode>, default: write
there are two available modes:
- write(Write/Overwrite): Overwrites or creates a new file.
- append (Append): Adds to the end of the file.
If you install it successfully and the optioner
command is not found, make sure to add $GOPATH/bin
to your environment variables.
When using Optioner
, you can customize its behavior with the following parameters:
-type
: Specifies the name of the target struct. This is a required parameter.-output
: Sets the output file path for the generated code. This is an optional parameter, with the default file name format beingopt_{StructName}_gen.go
, where{StructName}
is the name of the struct. The file is placed in the current directory by default.-with_prefix
: Sets the prefix for theWith{field_name}
function. This is an optional parameter with a default value ofWith{field_name}
. If a prefix is specified, such as User, the generated function name will becomeWithUser{field_name}
.-mode
: Determines the file writing mode, with two available options:write
(Write/Overwrite): Overwrites the file if it exists or creates a new one if it does not.append
(Append): Adds content to the end of an existing file.
You can directly use the optioner
command to generate functional options code for the corresponding struct, or you can use go generate
for bulk generation.
-
- First, assume you have prepared a Go file containing the struct for which you want to generate functional option pattern code. You can use the
opt
tag on the struct's fields to indicate which fields should be required parameters for theNew{FieldName}
function.
package example type User[T any, R any] struct { Name string `opt:"-"` NecGenericFiled T `opt:"-"` Age int Gender string GenericFiled R }
If a struct field is tagged with
opt
and its value is set to-
, the field becomes a required parameter for theNew{FieldName}
function, and aWith{FieldName}
function will not be generated for that field.Note: A
package
declaration is required. - First, assume you have prepared a Go file containing the struct for which you want to generate functional option pattern code. You can use the
-
- In the directory containing the struct definition file, execute the
optioner -type {StructName}
command, replacing{StructName}
with the actual name of your struct, for example,optioner -type User
. After running this command, theoptioner
tool automatically creates a defaultopt_user_gen.go
file and generates the functional options pattern code within it. The generated code structure is as follows:
// Generated by [optioner] command-line tool; DO NOT EDIT // If you have any questions, please create issues and submit contributions at: // https://github.com/chenmingyong0423/go-optioner package example type UserOption[T any, R any] func(*User[T, R]) func NewUser[T any, R any](name string, necGenericFiled T, opts ...UserOption[T, R]) *User[T, R] { user := &User[T, R]{ Name: name, NecGenericFiled: necGenericFiled, } for _, opt := range opts { opt(user) } return user } func WithAge[T any, R any](age int) UserOption[T, R] { return func(user *User[T, R]) { user.Age = age } } func WithGender[T any, R any](gender string) UserOption[T, R] { return func(user *User[T, R]) { user.Gender = gender } } func WithGenericFiled[T any, R any](genericFiled R) UserOption[T, R] { return func(user *User[T, R]) { user.GenericFiled = genericFiled } }
To customize the output file path for the generated code, you can specify the
output
andmode
parameters. - In the directory containing the struct definition file, execute the
Please ensure your project has been initialized with Go Modules or is correctly set up with GOPATH
, and that your project structure complies with the requirements of Go Modules or GOPATH
before running the go generate
command.
-
- First, assume you have prepared a Go file containing the struct for which you want to generate functional option pattern code. Add the comment
//go:generate optioner -type {StructName}
above the struct definition, replacing{StructName}
with the actual name of your struct, e.g., use//go:generate optioner -type User
for generating code for the User struct. You can use theopt
tag on the struct's fields to indicate which fields should be required parameters for theNew{FieldName}
function.
package example //go:generate optioner -type User type User[T any, R any] struct { Name string `opt:"-"` NecGenericFiled T `opt:"-"` Age int Gender string GenericFiled R }
If a struct field is tagged with
opt
and its value is set to-
, the field becomes a required parameter for theNew{FieldName}
function, and aWith{FieldName}
function will not be generated for that field.Note: A
package
declaration is required. - First, assume you have prepared a Go file containing the struct for which you want to generate functional option pattern code. Add the comment
-
- Run the
go generate
command in the directory containing the struct definition file. This command triggers theoptioner
tool and automatically creates a defaultopt_user_gen.go
file, generating functional options pattern code within it. The generated code structure is as follows:
// Generated by [optioner] command-line tool; DO NOT EDIT // If you have any questions, please create issues and submit contributions at: // https://github.com/chenmingyong0423/go-optioner package example type UserOption[T any, R any] func(*User[T, R]) func NewUser[T any, R any](name string, necGenericFiled T, opts ...UserOption[T, R]) *User[T, R ] { user := &User[T, R]{ Name: name, NecGenericFiled: necGenericFiled, } for _, opt := range opts { opt(user) } return user } func WithAge[T any, R any](age int) UserOption[T, R] { return func(user *User[T, R]) { user.Age = age } } func WithGender[T any, R any](gender string) UserOption[T, R] { return func(user *User[T, R]) { user.Gender = gender } } func WithGenericFiled[T any, R any](genericFiled R) UserOption[T, R] { return func(user *User[T, R]) { user.GenericFiled = genericFiled } }
To customize the output file path for the generated code, you can modify the content of
//go:generate optioner -type User
by specifying theoutput
andmode
parameters. - Run the
We welcome contributions from others! If you have any questions, suggestions for improvement, or have found a bug, please create an issue to discuss it with us. If you want to contribute code changes, please follow these steps:
1、Fork this repository and clone it to your local machine.
2、Create a new branch: git checkout -b feature/your-feature
.
3、Make your modifications or add new features on your branch.
4、Commit your changes: git commit -m "Describe your changes"
.
5、Push to your Fork repository: 1git push origin feature/your-feature1.
6、Create a Pull Request to merge your changes into the main repository.
Please ensure that your code follows the project's coding style and passes the tests.
This project is licensed under the Apache License.
If you have any questions or suggestions, you can contact us through the following methods:
- Email: [[email protected]]
- GitHub: [https://github.com/chenmingyong0423]