Skip to content

Commit 11f6018

Browse files
Revert "feat: update README"
This reverts commit 5a9f5db.
1 parent e1dbb74 commit 11f6018

File tree

2 files changed

+3
-126
lines changed

2 files changed

+3
-126
lines changed

README-zh_CN.md

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<h1 align="center">
22
go-optioner
33
</h1>
4-
go-optioner 是一个在 Go 代码中生成函数选项模式的工具。该工具可以根据给定的结构定义自动生成相应的选项代码。
4+
Go -optioner 是一个在 Go 代码中生成函数选项模式的工具。该工具可以根据给定的结构定义自动生成相应的选项代码。
55

66
---
77

@@ -17,70 +17,11 @@ Usage:
1717
go-optioner [flags]
1818
Flags:
1919
-type <struct name>
20-
-output <output path>, default: srcDir/opt_xxx_gen.go
2120
```
22-
如果你安装成功了,但是提示 `optioner` 命令找不到,请确认是否已将 `$GOPATH/bin` 添加到环境变量中。
23-
24-
# 使用教程
25-
你可以直接使用 `optioner` 命令生成对应结构体的 `functional options` 代码,也可以使用 `go generate` 进行批量生成。
26-
## optioner 命令
27-
- 1、首先,你需要创建一个包含需要生成函数选项模式代码的结构体的 `Go` 文件。在结构体字段中,您可以使用 `opt` 标签来控制是否为 `NewXXX()` 函数的必传参数和生成相应的函数。
28-
```go
29-
package example
30-
31-
type User struct {
32-
Name string `opt:"-"`
33-
Age int
34-
Gender string
35-
}
36-
37-
```
38-
如果字段定义了 `opt` 标签,并且值为 `-`,则它将作为 `NewXXX` 函数的必要参数,并且不会生成该字段的 `WithXXX` 函数。
39-
40-
注意:必须声明 `package`
41-
- 2、在包含结构体定义的文件目录下,执行 `optioner -type XXX` 命令,其中 `XXX` 是结构体的名称。执行命令之后,`optioner` 工具会根据结构体定义生成相应的函数选项模式代码。内容如下所示:
42-
```go
43-
// Generated by optioner -type User; DO NOT EDIT
44-
// If you have any questions, please create issues and submit contributions at:
45-
// https://github.com/chenmingyong0423/go-optioner
46-
47-
package example
48-
49-
type UserOption func(*User)
50-
51-
func NewUser(name string, opts ...UserOption) *User {
52-
user := &User{
53-
Name: name,
54-
}
55-
56-
for _, opt := range opts {
57-
opt(user)
58-
}
59-
60-
return user
61-
}
62-
63-
func WithAge(age int) UserOption {
64-
return func(user *User) {
65-
user.Age = age
66-
}
67-
}
68-
69-
func WithGender(gender string) UserOption {
70-
return func(user *User) {
71-
user.Gender = gender
72-
}
73-
}
74-
75-
```
76-
`optioner` 工具将会生成一个名为 `opt_xxx_gen.go` 的文件,其中 `xxx` 是结构体的名称,例如 `opt_user_gen.go`。该文件包含生成的函数选项代码,用于初始化结构体和设置结构体字段的值。
77-
## go generate 命令
78-
请注意,在执行 `go generate` 命令之前,确保您的项目已经初始化 `Go Modules` 或正确设置了 `GOPATH`,并且您的项目结构符合 `Go Modules``GOPATH` 的要求。
7921

22+
# 快速开始
8023
- 1、首先,你需要创建一个包含需要生成函数选项模式代码的结构体的 `Go` 文件。在结构体定义之上,你需要添加 `//go:generate optioner -type XXX` 的注释,其中 `XXX` 是结构体的名称。这样工具就能根据参数生成相应的代码。在结构体字段中,您可以使用 `opt` 标签来控制是否为 `NewXXX()` 函数的必传参数和生成相应的函数。
8124
```go
82-
package example
83-
8425
//go:generate optioner -type User
8526
type User struct {
8627
Name string `opt:"-"`
@@ -89,8 +30,6 @@ type User struct {
8930
}
9031
```
9132
如果字段定义了 `opt` 标签,并且值为 `-`,则它将作为 `NewXXX` 函数的必要参数,并且不会生成该字段的 `WithXXX` 函数。
92-
93-
注意:必须声明 `package`
9433
- 2、在包含结构体定义的文件目录下,执行 `go generate` 命令,这将调用 `optioner` 工具并根据结构体定义生成相应的函数选项模式代码。内容如下所示:
9534
```go
9635
// Generated by optioner -type User; DO NOT EDIT

README.md

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,10 @@ Usage:
1717
go-optioner [flags]
1818
Flags:
1919
-type <struct name>
20-
-output <output path>, default: srcDir/opt_xxx_gen.go
2120
```
22-
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-
type User struct {
33-
Name string `opt:"-"`
34-
Age int
35-
Gender string
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:
46-
// https://github.com/chenmingyong0423/go-optioner
47-
48-
package example
49-
50-
type UserOption func(*User)
51-
52-
func NewUser(name string, opts ...UserOption) *User {
53-
user := &User{
54-
Name: name,
55-
}
56-
57-
for _, opt := range opts {
58-
opt(user)
59-
}
60-
61-
return user
62-
}
63-
64-
func WithAge(age int) UserOption {
65-
return func(user *User) {
66-
user.Age = age
67-
}
68-
}
69-
70-
func WithGender(gender string) UserOption {
71-
return func(user *User) {
72-
user.Gender = gender
73-
}
74-
}
75-
76-
```
77-
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
8022
- 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.
8123
```go
82-
package example
83-
8424
//go:generate optioner -type User
8525
type User struct {
8626
Name string `opt:"-"`
@@ -89,8 +29,6 @@ type User struct {
8929
}
9030
```
9131
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.
9432
- 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:
9533
```go
9634
// Generated by optioner -type User; DO NOT EDIT

0 commit comments

Comments
 (0)