Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed May 12, 2024
1 parent d8a7c84 commit d77e9d9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
85 changes: 84 additions & 1 deletion docs/zh/guide/auto-gen-priest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,87 @@ sidebar: auto

# 自动生成Priest

> todo
## 小试牛刀
`gone priest`命令可以扫描代码中的特殊注释`//go:gone`生成Priest函数;`//go:gone`暂时只能用于标记`func () gone.Goner``func () (gone.Goner, gone.GonerId)`这两种形式的函数,函数必须是公开(以大写字母开头)。

### 1. 安装gone辅助工具
执行如下命令:
```bash
go install github.com/gone-io/gone/tools/gone@latest
```
gone更多内容参考文档 [gone辅助工具](../references/gone-tool.md)

### 2. 编写Goner
创建项目,创建文件:
```bash
mkdir demo
cd demo
go mod init demo
touch demo.go
```

编辑demo.go文件代码如下:
```go
package demo

import "github.com/gone-io/gone"

//go:gone
func NewDemo() gone.Goner {
return &Demo{}
}

type Demo struct {
gone.Flag
}
```

### 3. 生成代码
执行
```bash
gone priest -s ./ -f Priest -o priest.go -p demo
```
将生成文件:priest.go
内容如下:
```go
// Code generated by gone; DO NOT EDIT.
package demo
import (
"github.com/gone-io/gone"
)

func Priest(cemetery gone.Cemetery) error {
cemetery.Bury(NewDemo())
return nil
}
```

## 最佳实践
- 创建一个MasterPriest函数,用于Gone框架的启动;
- 在MasterPriest函数中,调用项目依赖包的Priest函数;
- 将“gone priest”命令放到MasterPriest函数的`//go:generate`注释后,例如:
```go
package internal

import (
"github.com/gone-io/gone"
"github.com/gone-io/gone/goner"
)

//go:generate gone priest -s . -p $GOPACKAGE -f Priest -o priest.go
func MasterPriest(cemetery gone.Cemetery) error {
//调用项目依赖包的Priest函数
_ = goner.GinPriest(cemetery)

//调用生成的Priest函数
_ = Priest(cemetery)
return nil
}
```
> tips: 运行`go generate ./...` 会执行 所有`//go:generate`注释后的命令;
- 在MasterPriest函数中,调用调用生成的Priest函数;
- git忽略掉生成文件,将生成的文件加入到`.gitignore`文件中。

[Demo](https://github.com/gone-io/examples/tree/main/empty)是一个可供参考的web空白项目;也可以运行`gone create web-app`生成一个空白项目,参考[快速开始
](https://goner.fun/zh/quick-start/)
2 changes: 1 addition & 1 deletion docs/zh/quick-start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type IDemo interface {
服务的逻辑实现,放到`internal/module`目录分模块实现。


## 数据库
## [数据库](https://goner.fun/zh/guide/xorm.html)


## 服务启动流程
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/references/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ sidebar: auto

# 参考文档

## [gone辅助工具](./gone-tool.md)

## [http 注入说明](./http-inject.md)
21 changes: 21 additions & 0 deletions docs/zh/references/gone-tool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
sidebar: auto
---

# gone辅助工具

## 安装
执行如下命令:
```bash
go install github.com/gone-io/gone/tools/gone@latest
```

## 使用

### 测试是否正常安装
成功安装后,执行`gone -h`,建获得如下结果:
![gone 命令结果](../../img/image3.png)

### 生成项目

### 生成Priest函数

0 comments on commit d77e9d9

Please sign in to comment.