gin框架的几种热加载方法,你值得拥有

Go语言精选

共 12351字,需浏览 25分钟

 · 2021-03-02

前言

今天给大介绍几种热加载的方法,大大提高我们的开发效率。

本文的所有项目效果演示都是基于gin_jwt_swagger开源项目,项目地址:https://github.com/asong2020/Golang_Dream/tree/master/Gin/gin_jwt_swagger。欢迎Star与使用。

什么是热加载

如果你是一名python开发者,应该很熟悉这个。我们在Flask或者Django框架下开发都是支持实时加载的,当我们对代码进行修改时,程序能够自动重新加载并执行,这在我们开发中是非常便利的,可以快速进行代码测试,省去了每次手动重新编译。

如果你是一名JAVA开发者,不仅会听过热加载,热部署会跟着一块出现。热部署一般是指容器(支持多应用)不重启,单独启动单个应用。热加载一般指重启应用(JVM),单独重新更新某个类或者配置文件。

知道了什么是热加载了,想在项目开发中使用,该怎么做呢?下面就来介绍几种方法,想用哪个就用哪个,就是这么豪横。哼!!!

1. Air

github地址:https://github.com/cosmtrek/air Star:1.8k

它具有以下特性:

  • 彩色日志输出

  • 自定义构建或二进制命令

  • 支持忽略子目录

  • 启动后支持监听新目录

  • 更好的构建过程

安装

1$ go get -u github.com/cosmtrek/air

使用

为了我们能够好的使用命令行操作,我们需要把alias air='~/.air'加到你的.bashrc.zshrc中,根据你的系统进行选择,因为我是mac,所以我将alias air='~/.air'加到了vim ~/.zshrc中了。

然后命令下执行如下操作

 1# 1. 进入你自己的项目目录
2$ cd /your_project
3# 2. 查找你的项目中是否存在 `.air.conf` 配置文件
4$ air -c .air.conf
5# 3. 没有则创建一个
6$ touch .air.conf
7# 4. 复制下面示例 `air.conf.example`到你的 `.air.conf`
8
9#
 5. 启动热加载
10$ air 
11# 6. 启动热加载 带打印log
12$ air -d

air.conf.example示例

参考:传送门

 1root = "."
2tmp_dir = "tmp"
3
4[build]
5# Just plain old shell command. You could use `make` as well.
6cmd = "go build -o ./tmp/main ."
7# Binary file yields from `cmd`.
8bin = "tmp/main"
9# Customize binary.
10full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
11# Watch these filename extensions.
12include_ext = ["go""tpl""tmpl""html"]
13# Ignore these filename extensions or directories.
14exclude_dir = ["assets""tmp""vendor""frontend/node_modules"]
15# Watch these directories if you specified.
16include_dir = []
17# Exclude files.
18exclude_file = []
19# This log file places in your tmp_dir.
20log = "air.log"
21# It's not necessary to trigger build each time file changes if it's too frequent.
22delay = 1000 # ms
23# Stop running old binary when build errors occur.
24stop_on_error = true
25# Send Interrupt signal before killing process (windows does not support this feature)
26send_interrupt = false
27# Delay after sending Interrupt signal
28kill_delay = 500 # ms
29
30[log]
31# Show log time
32time = false
33
34[color]
35# Customize each part's color. If no color found, use the raw app log.
36main = "magenta"
37watcher = "cyan"
38build = "yellow"
39runner = "green"
40
41[misc]
42# Delete tmp directory on exit
43clean_on_exit = true
44

效果演示

2. Fresh

github地址:https://github.com/gravityblast/fresh Star:2.8K

Fresh是一个命令行工具,每次保存Go或模版文件时,该工具都会生成或重新启动Web应用程序。Fresh将监视文件事件,并且每次创建/修改/删除文件时,Fresh都会生成并重新启动应用程序。如果go build返回错误,它会将记录在tmp文件夹中。

安装

1go get github.com/pilu/fresh

使用

1# 进入你的项目目录
2$ cd /your_project
3# 启动
4$ fresh

效果演示

3. bee

github地址:https://github.com/beego/bee Satr:1.1K

bee是beego框架的热编译工具,同样可以对GIN框架进行热编译,使用起来很方便,功能也有很多,这里就不展开了,喜欢的可以去看文档,解锁更多玩法。

安装

1# To install bee use the go get command:
2$ go get github.com/beego/bee
3# If you already have bee installed, updating bee is simple:
4$ go get -u github.com/beego/bee
5# Then you can add bee binary to PATH environment variable in your ~/.bashrc or ~/.bash_profile file:
6$ export PATH=$PATH:<your_main_gopath>/bin

使用

1# 进入你的项目目录,注意:使用bee 项目必须要在GOPATH目录下
2$ cd /your_project
3# 运行程序
4$ bee run

效果演示

4. gowatch

github地址:https://github.com/silenceper/gowatch Star

Go程序热编译工具,通过监听当前目录下的相关文件变动,进行实时编译。

安装

1$ go get github.com/silenceper/gowatch

使用

安装完成后可以直接使用gowatch命令,命令行参数如下:

  • -o : 非必须,指定build的目标文件路径

  • -p : 非必须,指定需要build的package(也可以是单个文件)

  • -args: 非必须,指定程序运行时参数,例如:-args='-host=:8080,-name=demo'

  • -v: 非必须,显示gowatch版本信息

1$ gowatch -o ./bin/demo -p ./cmd/demo

gowatch可以修改配置文件gowatch.yml

大部分情况下,不需要更改配置,直接执行gowatch命令就能满足的大部分的需要,但是也提供了一些配置用于自定义,在执行目录下创建gowatch.yml文件:

 1# gowatch.yml 配置示例
2
3# 当前目录执行下生成的可执行文件的名字,默认是当前目录名
4appname: "test"
5# 指定编译后的目标文件目录
6output: /bin/demo
7# 需要追加监听的文件名后缀,默认只有'.go'文件
8watch_exts:
9    - .yml
10# 需要监听的目录,默认只有当前目录
11watch_paths:
12    - ../pk
13# 在执行命令时,需要增加的其他参数
14cmd_args:
15    - arg1=val1
16# 在构建命令时,需要增加的其他参数
17build_args:
18    - -race
19# 需要增加环境变量,默认已加载当前环境变量
20envs:
21    - a=b
22# 是否监听 ‘vendor’ 文件夹下的文件改变
23vendor_watch: false
24# 不需要监听的目录名字
25excluded_paths:
26    - path
27# main 包路径,也可以是单个文件,多个文件使用逗号分隔
28build_pkg: ""
29# build tags
30build_tags: ""
31
32# 是否禁止自动运行
33disable_run: false

项目演示

5. gin

github地址:https://github.com/codegangsta/gin Star:3.4K

gin是用于实时重新加载Go Web应用程序的简单命令行实用程序。只需gin在您的应用程序目录中运行,您的网络应用程序将 gin作为代理提供。gin检测到更改后,将自动重新编译您的代码。您的应用在下次收到HTTP请求时将重新启动。

gin 坚持“沉默就是黄金”的原则,因此,只有在出现编译器错误或在错误发生后成功进行编译时,它才会抱怨。

安装

1$ go get github.com/codegangsta/gin
2# Then verify that gin was installed correctly:
3$ gin -h

使用

1$ gin run main.go

Options:

 1 --laddr value, -l value       listening address for the proxy server
2   --port value, -p value        port for the proxy server (default: 3000)
3   --appPort value, -a value     port for the Go web server (default: 3001)
4   --bin value, -b value         name of generated binary file (default: "gin-bin")
5   --path value, -t value        Path to watch files from (default: ".")
6   --build value, -d value       Path to build files from (defaults to same value as --path)
7   --excludeDir value, -x value  Relative directories to exclude
8   --immediate, -i               run the server immediately after it's built
9   --all                         reloads whenever any file changes, as opposed to reloading only on .go file change
10   --godep, -g                   use godep when building
11   --buildArgs value             Additional go build arguments
12   --certFile value              TLS Certificate
13   --keyFile value               TLS Certificate Key
14   --logPrefix value             Setup custom log prefix
15   --notifications               enable desktop notifications
16   --help, -h                    show help
17   --version, -v                 print the version

项目演示

6. realize

github地址:https://github.com/oxequa/realize Star:3.8K

realize是Golang的实时重载和任务运行器。它主要功能如下:

  • 高性能实时刷新。

  • 同时管理多个项目。

  • 通过自定义扩展名和路径观看。

  • 支持所有Go命令。

  • 在不同的Go版本之间切换。

  • 项目的自定义环境变量。

  • 在文件更改前后或全局执行自定义命令。

  • 将日志和错误导出到外部文件。

  • 分步项目初始化。

  • 重新设计的面板,显示构建错误,控制台输出和警告。

安装

1$ go get github.com/oxequa/realize

我直接这么安装失败了,出现了错误,我的GO版本是1.14,所以使用如下方式安装成功:

1$  GO111MODULE=off go get github.com/oxequa/realize

使用

1# 首先进行初始化 默认配置即可
2$ realize init
3# 执行项目
4$ realize start
5# 添加命令
6$ realize add
7# 删除命令
8$ realize init

Options:

 1--name="name"               -> Run by name on existing configuration
2--path="realize/server"     -> Custom Path (if not specified takes the working directory name)
3--generate                  -> Enable go generate
4--fmt                       -> Enable go fmt
5--test                      -> Enable go test
6--vet                       -> Enable go vet
7--install                   -> Enable go install
8--build                     -> Enable go build
9--run                       -> Enable go run
10--server                    -> Enable the web server
11--open                      -> Open web ui in default browser
12--no-config                 -> Ignore an existing config / skip the creation of a new one

Examples:

1$ realize start
2$ realize start --path="mypath"
3$ realize start --name="realize" --build
4$ realize start --path="realize" --run --no-config
5$ realize start --install --test --fmt --no-config
6$ realize start --path="/Users/username/go/src/github.com/oxequa/realize-examples/coin/"

realize 使用方法比较多,感兴趣的可以去官方文档学习。

项目演示

总结

好啦,这一篇文章到此就结束啦。总结了6种热加载的方法,每一种都有各自的特点,根据喜欢的喜好,选择一款呗,大大提高我们的开发效率呦!!!


浏览 30
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报