README ¶
go-optioner
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 | 中文简体
Installation
- 1、
go install github.com/chenmingyong0423/go-optioner/cmd/optioner@latest
- 2、Run the
optioner
command to check if the installation is successful.
> optioner
go-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
If you install it successfully and the optioner
command is not found, make sure to add $GOPATH/bin
to your environment variables.
Usage
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.
optioner commond
- 1、First, you need to create a Go file that includes the struct for which you want to generate the function options pattern code. In the struct fields, you can use the
opt
tag to control whether the field is a required parameter for theNewXXX()
function and generate the corresponding functions.
package example
type User struct {
Name string `opt:"-"`
Age int
Gender string
}
If a field is tagged with opt
and its value is -
, it will be treated as a required parameter for the NewXXX
function, and the corresponding WithXXX
function will not be generated.
Note: You must declare package.
- 2、In the directory where the struct definition file is located, execute the
optioner -type XXX
command, whereXXX
is the name of the struct. After running the command, theoptioner
tool will generate the corresponding function options pattern code based on the struct definition. The generated code will look like this:
// Generated by optioner -type User; 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 func(*User)
func NewUser(name string, opts ...UserOption) *User {
user := &User{
Name: name,
}
for _, opt := range opts {
opt(user)
}
return user
}
func WithAge(age int) UserOption {
return func(user *User) {
user.Age = age
}
}
func WithGender(gender string) UserOption {
return func(user *User) {
user.Gender = gender
}
}
The optioner
tool will generate a file named opt_xxx_gen.go
, where xxx
is the name of the struct, for example, opt_user_gen.go
. This file contains the generated function options code to initialize the struct and set the struct fields' values.
go generate commond
Please note that before executing the go generate
command, ensure that your project has been initialized with Go Modules
or that GOPATH
is correctly set, and your project's structure complies with the requirements of Go Modules
or GOPATH
.
- 1、First, you need to create a
Go
file containing the struct definition for which you want to generate the function options pattern. Above the struct definition, add the//go:generate optioner -type XXX
comment, whereXXX
is the name of the struct. This will enable the tool to generate the corresponding code based on the provided parameter. In the struct fields, you can use theopt
tag to control whether to generate the corresponding functions and whether the field is a required parameter for theNewXXX()
function.
package example
//go:generate optioner -type User
type User struct {
Name string `opt:"-"`
Age int
Gender string
}
If a field is tagged with opt
and its value is -
, it will be treated as a required parameter for the NewXXX
function, and the corresponding WithXXX
function will not be generated.
Note: You must declare package.
- 2、In the directory where the struct definition file is located, run the
go generate
command. This will call theoptioner
tool and generate the corresponding function options pattern code based on the struct definition. The generated code will be similar to the following:
// Generated by optioner -type User; 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 func(*User)
func NewUser(name string, opts ...UserOption) *User {
user := &User{
Name: name,
}
for _, opt := range opts {
opt(user)
}
return user
}
func WithAge(age int) UserOption {
return func(user *User) {
user.Age = age
}
}
func WithGender(gender string) UserOption {
return func(user *User) {
user.Gender = gender
}
}
The optioner
tool will generate a file named opt_xxx_gen.go
, where xxx
is the name of the struct, for example, opt_user_gen.go
. This file contains the generated function options code to initialize the struct and set the struct fields' values.
How to Contribute
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.
License
This project is licensed under the Apache License.
Contact Us
If you have any questions or suggestions, you can contact us through the following methods:
- Email: [chenmingyong5873@126.com]
- GitHub: [https://github.com/chenmingyong0423]