-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,13 +49,13 @@ See [more examples here](https://github.com/Bin-Huang/newc/tree/master/test) | |
Without manual installation, just add this comment line to the struct. Go will automatically install this tool if missing. | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
``` | ||
|
||
For example: | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
type UserService struct { | ||
baseService | ||
userRepository *repositories.UserRepository | ||
|
@@ -65,7 +65,7 @@ type UserService struct { | |
|
||
This is very useful, especially in teamwork. **It can run without manual installation. It doesn't break the work of other people who don't have installed this tool in collaboration.** | ||
|
||
## Return value instead reference | ||
## How to return value instead reference? | ||
|
||
Add `--value` parameter | ||
|
||
|
@@ -89,7 +89,7 @@ func NewConfig(debug bool) Config { | |
} | ||
``` | ||
|
||
## Call an initializer | ||
## How to call an initializer in constructor? | ||
|
||
1. Add `--init` parameter | ||
2. Write an `init` method for the struct | ||
|
@@ -123,6 +123,28 @@ func NewController(logger *zap.Logger, debug bool) *Controller { | |
} | ||
``` | ||
|
||
## How to ignore some fields when generating constructor code? | ||
|
||
Add a tag `newc:"-"` to fields that need to be ignored | ||
|
||
```go | ||
type Forbidden struct { | ||
Msg string | ||
Status int `newc:"-"` | ||
} | ||
``` | ||
|
||
Generated code: | ||
|
||
```go | ||
// NewForbidden Create a new Forbidden | ||
func NewForbidden(msg string) *Forbidden { | ||
return &Forbidden{ | ||
Msg: msg, | ||
} | ||
} | ||
``` | ||
|
||
## If you think the `go:generate` comment is too long... | ||
|
||
Some suggestions: | ||
|
@@ -147,7 +169,7 @@ Don't worry about the imports, variable naming, and code style in the generated | |
It doesn't break the work of other people who don't have installed this tool in collaboration. Go will automatically install this tool if missing. | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
``` | ||
|
||
## Sponsoring | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,13 +49,13 @@ func NewUserService(baseService baseService, userRepository *repositories.UserRe | |
无需手动安装,只需要给结构体添加下面这行注释就行。Go 会在缺失时自动下载这个工具。 | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
``` | ||
|
||
比如这样: | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
type UserService struct { | ||
baseService | ||
userRepository *repositories.UserRepository | ||
|
@@ -65,7 +65,7 @@ type UserService struct { | |
|
||
这个方式非常有用,尤其在团队开发中。**就算其他同事没有安装这个工具,这么做也能正常运行,不会影响到其他人的工作**。 | ||
|
||
## 返回结构体的值,而不是引用 | ||
## 如何返回结构体的值,而不是引用? | ||
|
||
使用 `--value` 参数 | ||
|
||
|
@@ -123,6 +123,28 @@ func NewController(logger *zap.Logger, debug bool) *Controller { | |
} | ||
``` | ||
|
||
## 如果在生成构造器时忽略掉一些字段? | ||
|
||
给需要忽略的字段添加 `newc:"-"` 标签 | ||
|
||
```go | ||
type Forbidden struct { | ||
Msg string | ||
Status int `newc:"-"` | ||
} | ||
``` | ||
|
||
生成代码: | ||
|
||
```go | ||
// NewForbidden Create a new Forbidden | ||
func NewForbidden(msg string) *Forbidden { | ||
return &Forbidden{ | ||
Msg: msg, | ||
} | ||
} | ||
``` | ||
|
||
## 如果你觉得这条注释太长…… | ||
|
||
一些建议: | ||
|
@@ -147,7 +169,7 @@ func NewController(logger *zap.Logger, debug bool) *Controller { | |
就算其他同事没有安装这个工具,这么做也不会影响到他们的工作。因为 Go 会在必要时自动安装这个工具。 | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
//go:generate go run github.com/Bin-Huang/[email protected].3 | ||
``` | ||
|
||
## 赞赏 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,3 @@ package config | |
type Config struct { | ||
Debug bool | ||
} | ||
|
||
// DebugConfig ... | ||
type DebugConfig struct { | ||
Debug bool | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package errors | ||
|
||
import "fmt" | ||
|
||
// Forbidden ... | ||
//go:generate go run ../../../newc --value --init | ||
type Forbidden struct { | ||
Msg string `bson:"msg" json:"msg"` | ||
Status int `bson:"status" json:"status" newc:"-"` | ||
} | ||
|
||
func (e *Forbidden) init() { | ||
e.Status = 403 | ||
} | ||
|
||
// Error ... | ||
func (e Forbidden) Error() string { | ||
return fmt.Sprintf("forbidden") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package errors | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNewForbidden(t *testing.T) { | ||
value := NewForbidden("msg") | ||
if value.Status != 403 { | ||
t.Errorf("NewForbidden should calling init method") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters