Skip to content

Commit

Permalink
doc: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Aug 5, 2022
1 parent 5ed8092 commit 7eb044e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ A command-line tool to generate constructor code for a struct. It don't need man
It don't need a manual installation. Just add this comment line to the struct you want to generate a constructor.

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.6.1
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.0
```

For example:

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.6.1
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.0
type UserService struct {
baseService
userRepository *repositories.UserRepository
proRepository *repositories.ProRepository
}
```

after `go generate ./...`, `go test` or `go build`, you get this:
After `go generate ./...` you will get this:

```go
// constructor_gen.go
Expand Down Expand Up @@ -59,6 +59,46 @@ type UserService struct {
}
```

## Wanna initialize something in the constructor?

1. Add `--init` parameter
2. Write an `init` method for the struct

```go
//go:generate go run github.com/Bin-Huang/[email protected] --init
type Controller struct {
logger *zap.Logger
debug bool
}

func (c *Controller) init() {
c.logger = c.logger.With(zap.String("tag", "this-special-controller"))
c.debug = true
}
```

Generated code:

```go
// constructor_gen.go

// NewController Create a new Controller
func NewController(logger *zap.Logger, debug bool) *Controller {
s := &Controller{
logger: logger,
debug: debug,
}
s.init()
return s
}
```

## If you think the "magic comment" is too long...

Some suggestions:
1. Add a code snippest in your editor/IDE for the tool (suggested)
2. [Install this tool manually](#can-it-be-installed-locally)

## Features & Motivation

**It makes your code easier to write and maintain**.
Expand Down
45 changes: 43 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Doc: [English](README.md) | **中文**
它的使用方式非常简单,不用专门手动安装,只需要在结构体添加下面这行代码注释就能工作。

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.6.1
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.0
```

举个例子:

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.6.1
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.0
type UserService struct {
baseService
userRepository *repositories.UserRepository
Expand Down Expand Up @@ -61,6 +61,47 @@ type UserService struct {
}
```

## 想在构造时做些初始化?

1. 加上 `--init` 参数
2. 为结构体实现一个 `init` 方法

```go
//go:generate go run github.com/Bin-Huang/[email protected] --init
type Controller struct {
logger *zap.Logger
debug bool
}

func (c *Controller) init() {
c.logger = c.logger.With(zap.String("tag", "this-special-controller"))
c.debug = true
}
```

生成代码:

```go
// constructor_gen.go

// NewController Create a new Controller
func NewController(logger *zap.Logger, debug bool) *Controller {
s := &Controller{
logger: logger,
debug: debug,
}
s.init()
return s
}
```

## 如果你觉得这条注释太长……

一些建议:

1. (推荐)把它加进你的编辑器/IDE的快捷代码片段(code snippest)里
2. 手动安装这个工具

## 功能特性与设计理念

**它能让你的代码更容易编写和维护**.
Expand Down

0 comments on commit 7eb044e

Please sign in to comment.