项目地址: https://github.com/cosmtrek/air/blob/master/README-zh_cn.md
Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只需在你的项目根目录下输入 air
,然后把它放在一边,专注于你的代码即可。
例子
创建一个gin项目
创建一个gin项目,这里以gin为例,其他go web项目都可以。
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "golang-tech-stack.com",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
但是这么部署的缺点就在于,你每次修改都要手动进行重新编译,效率十分低下,显然是不符合我们摸鱼原则的§( ̄▽ ̄)§
下载安装air
使用 Go 的版本为 1.16 或更高:
go install github.com/cosmtrek/air@latest
注意:这里要用
install
,而不是get
,install
会生成可执行文件,并且保证你的gopath/bin
,已经配置到了系统环境变量下面。
启动air
air
初始化air
air init
测试
现在修改你的项目,保存后会自动热部署。
自定义
在命令行中执行
air init
将会在当前目录下生成.air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "tmp\\main.exe"
cmd = "go build -o ./tmp/main.exe ."
delay = 0
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true
问题
遇到 “command not found: air” 或 “No such file or directory” 该怎么办?
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.bashrc
echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> ~/.bashrc
source ~/.bashrc