1 Star 0 Fork 25

泊舟 / gfstudy

forked from goflyfox / gfstudy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
18.GoFrame工具链之项目构建.md 7.26 KB
一键复制 编辑 原始数据 按行查看 历史
zcool321@sina.com 提交于 2020-04-29 01:42 . add gf tool chain

GoFrame工具链之项目构建

项目结构

推荐的Go业务型项目目录结构如下:

/
├── app
│   ├── api
│   ├── model
│   └── service
├── boot
├── config
├── docker
├── document
├── i18n
├── library
├── public
├── router
├── template
├── vendor
├── Dockerfile
├── go.mod
└── main.go
目录/文件名称 说明 描述
app 业务逻辑层 所有的业务逻辑存放目录。
- api 业务接口 接收/解析用户输入参数的入口/接口层。
- model 数据模型 数据管理层,仅用于操作管理数据,如数据库操作。
- service 逻辑封装 业务逻辑封装层,实现特定的业务需求,可供不同的包调用。
boot 初始化包 用于项目初始化参数设置,往往作为main.go中第一个被import的包。
config 配置管理 所有的配置文件存放目录。
docker 镜像文件 Docker镜像相关依赖文件,脚本文件等等。
document 项目文档 Document项目文档,如: 设计文档、帮助文档等等。
i18n I18N国际化 I18N国际化配置文件目录。
library 公共库包 公共的功能封装包,往往不包含业务需求实现。
public 静态目录 仅有该目录下的文件才能对外提供静态服务访问。
router 路由注册 用于路由统一的注册管理。
template 模板文件 MVC模板文件存放的目录。
vendor 第三方包 第三方依赖包存放目录(可选, 未来会被淘汰)。
Dockerfile 镜像描述 云原生时代用于编译生成Docker镜像的描述文件。
go.mod 依赖管理 使用Go Module包管理的依赖描述文件。
main.go 入口文件 程序入口文件。

在实践中,小伙伴们可以根据实际情况增删目录。

初始化项目init

D:\17.gfcli>gf init -h
USAGE
    gf init [NAME]

ARGUMENT
    NAME  name for current project, not necessary, default name is 'gf-app'

EXAMPLES
    gf init
    gf init my-project-name


D:\17.gfcli>gf init gfcli
initializing...
initialization done!
you can now run 'gf run main.go' to start your journey, enjoy!

D:\17.gfcli> tree /f
卷 Data 的文件夹 PATH 列表
卷序列号为 DA91-D877
D:.
│  .gitattributes
│  .gitignore
│  Dockerfile
│  go.mod
│  go.sum
│  main.go
│  README.MD

├─app
│  ├─api
│  │  └─hello
│  │          hello.go
│  │
│  ├─model
│  │      .gitkeep
│  │
│  └─service
│          .gitkeep

├─boot
│      .gitkeep
│      boot.go

├─config
│      .gitkeep
│      config.toml

├─docker
│      .gitkeep

├─document
│      .gitkeep

├─i18n
│      .gitkeep

├─public
│  ├─html
│  │      .gitkeep
│  │
│  ├─plugin
│  │      .gitkeep
│  │
│  └─resource
│      ├─css
│      │      .gitkeep
│      │
│      ├─image
│      │      .gitkeep
│      │
│      └─js
│              .gitkeep

├─router
│      .gitkeep
│      router.go

└─template
        .gitkeep

热编译运行项目run

D:\17.gfcli> go install
go: downloading github.com/gogf/gf v1.12.2
go: downloading gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
go: downloading github.com/mattn/go-runewidth v0.0.9

D:\17.gfcli> gf run .\main.go
2020-04-26 22:58:09.022 [DEBU] [ghttp] SetServerRoot path: D:\17.gfcli\public

  SERVER  | DOMAIN  | ADDRESS | METHOD | ROUTE |          HANDLER          | MIDDLEWARE
|---------|---------|---------|--------|-------|---------------------------|------------|
  default | default | :8199   | ALL    | /     | gfcli/app/api/hello.Hello |
|---------|---------|---------|--------|-------|---------------------------|------------|

2020-04-26 22:58:09.041 16764: http server started listening on [:8199]
exit status 2

交叉编译build

D:\17.gfcli> gf build -h
USAGE
    gf build FILE [OPTION]

ARGUMENT
    FILE  building file path.

OPTION
    -n, --name       output binary name
    -v, --version    output binary version
    -a, --arch       output binary architecture, multiple arch separated with ','
    -s, --system     output binary system, multiple os separated with ','
    -o, --output     output binary path, used when building single binary file
    -p, --path       output binary directory path, default is './bin'
        -e, --extra      extra custom "go build" options
    -m, --mod        like "-mod" option of "go build", use "-m none" to disable go module
    --swagger        auto parse and pack swagger into boot/data-swagger.go before building.
    --pack           auto pack config,public,template folder into boot/data-packed.go before building.

EXAMPLES
    gf build main.go
    gf build main.go --swagger
    gf build main.go --pack
    gf build main.go -m none --pack
    gf build main.go -n my-app -a all -s all
    gf build main.go -n my-app -a amd64,386 -s linux -p .
    gf build main.go -n my-app -v 1.0 -a amd64,386 -s linux,windows,darwin -p ./docker/bin

DESCRIPTION
    The "build" command is most commonly used command, which is designed as a powerful wrapper for
    "go build" command for convenience cross-compiling usage.
    It provides much more features for building binary:
    1. Cross-Compiling for many platforms and architectures.
    2. Configuration file support for compiling.
    3. Build-In Variables.

PLATFORMS
    darwin    386
    darwin    amd64
    freebsd   386
    freebsd   amd64
    freebsd   arm
    linux     386
    linux     amd64
    linux     arm
    linux     arm64
    linux     ppc64
    linux     ppc64le
    linux     mips
    linux     mipsle
    linux     mips64
    linux     mips64le
    netbsd    386
    netbsd    amd64
    netbsd    arm
    openbsd   386
    openbsd   amd64
    openbsd   arm
    windows   386
    windows   amd64

D:\17.gfcli> gf build main.go -n my-app -a amd64,386 -s linux,windows
2020-04-27 00:29:56.789 start building...
2020-04-27 00:29:56.790 go build -o ./bin/linux_386/my-app main.go
2020-04-27 00:30:00.745 go build -o ./bin/linux_amd64/my-app main.go
2020-04-27 00:30:04.317 go build -o ./bin/windows_386/my-app.exe main.go
2020-04-27 00:30:08.286 go build -o ./bin/windows_amd64/my-app.exe main.go
2020-04-27 00:30:11.449 done!

D:\17.gfcli> tree /f .\bin
卷 Data 的文件夹 PATH 列表
卷序列号为 DA91-D877
D:\17.GFCLI\BIN
├─linux_386
│      my-app

├─linux_amd64
│      my-app

├─windows_386
│      my-app.exe

└─windows_amd64
        my-app.exe
1
https://gitee.com/ylmtest/gfstudy.git
git@gitee.com:ylmtest/gfstudy.git
ylmtest
gfstudy
gfstudy
master

搜索帮助