You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you install it successfully and the `optioner` command is not found, make sure to add `$GOPATH/bin` to your environment variables.
23
-
24
-
# Usage
25
-
26
-
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.
27
-
## optioner commond
28
-
- 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 the `NewXXX()` function and generate the corresponding functions.
29
-
```go
30
-
package example
31
-
32
-
typeUserstruct {
33
-
Namestring`opt:"-"`
34
-
Ageint
35
-
Genderstring
36
-
}
37
-
38
-
```
39
-
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.
40
-
41
-
Note: You must declare package.
42
-
- 2、In the directory where the struct definition file is located, execute the `optioner -type XXX` command, where `XXX` is the name of the struct. After running the command, the `optioner` tool will generate the corresponding function options pattern code based on the struct definition. The generated code will look like this:
43
-
```go
44
-
// Generated by optioner -type User; DO NOT EDIT
45
-
// If you have any questions, please create issues and submit contributions at:
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.
78
-
## go generate commond
79
-
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`.
21
+
# Quick Start
80
22
- 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, where `XXX` 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 the `opt` tag to control whether to generate the corresponding functions and whether the field is a required parameter for the `NewXXX()` function.
81
23
```go
82
-
package example
83
-
84
24
//go:generate optioner -type User
85
25
typeUserstruct {
86
26
Namestring`opt:"-"`
@@ -89,8 +29,6 @@ type User struct {
89
29
}
90
30
```
91
31
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.
92
-
93
-
Note: You must declare package.
94
32
- 2、In the directory where the struct definition file is located, run the `go generate` command. This will call the `optioner` tool and generate the corresponding function options pattern code based on the struct definition. The generated code will be similar to the following:
0 commit comments