对于 web 后端开发而言,往往会在开发环境中,一边运行服务,另一边同时修改代码。我们往往希望,在修改代码并保存后,运行的服务能够自动更新,便于我们观察代码修改的效果。

对于脚本语言来说,它们没有编译的过程,重新加载服务是比较容易做到的。而对于 Go 语言来说,热重载并不是一个那么容易实现的功能,web 框架往往也没有内置该功能。Air,为 Go 语言提供了基于配置文件的热重载功能,能够提高开发效率。

go air

简介

Air,是 cosmtrek 在 Github 上开源的 Go 语言热重载工具,仓库位于 https://github.com/cosmtrek/air,目前版本为 v1.27.2。

Air 通过监听代码文件或目录的变化,进行自动编译和重启程序,大大提高了 Go 程序的开发效率。相比于其他 Golang 热重载工具,air:

提供了颜色丰富的日志输出能够自定义编译命令支持把特定的文件夹剔除出监听目标支持监听在 air 启动后再新建的目录提供了更好的编译过程

go air

使用

Air 可以全局安装,使用项目提供的 install.sh 脚本:

# 安装到 $(go env GOPATH)/bin/aircurl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin# 安装到 ./bin/curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s

也可以使用 docker 镜像 cosmtrek/air:

docker run -it --rm -w "<PROJECT>" -e "air_wd=<PROJECT>" -v $(pwd):<PROJECT> -p <PORT>:<APP SERVER PORT> cosmtrek/air -c <CONF>

对于单一项目中,也可以直接使用 Go Module 安装:

go get -u github.com/cosmtrek/air

我们来看一个最简单的例子。首先,我们创建一个简单的服务器程序:

package mainimport ( "fmt" "log" "net/http")func index(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world!")}func main() { mux := http.NewServeMux() mux.HandleFunc("/", index) server := &http.Server{ Handler: mux, Addr: ":8080", } log.Fatal(server.ListenAndServe())}

我们按正常的流程运行程序:

$ go run main.go

在浏览器中访问 localhost:8080,就能看到 Hello, world! 的响应了。

使用 air,我们把 go run 替代为 air 命令:

$ air

Air 会自动编译,启动程序,并监听当前目录中的文件修改。当把代码中的 Hello, world! 改为 Hello, go!,之后,无需停止并重新编译,就能自动重新加载修改后的项目。访问服务器地址,响应变为了 Hello, go!。

go ar

直接执行 air 命令,使用的是默认配置。如果需要更为定制化的配置,可以以 air 项目中提供的 air_example.toml 配为模版进行定制修改。

# Config file for [Air](https://github.com/cosmtrek/air) in TOML format# Working directory# . or absolute path, please note that the directories following must be under root.root = "."tmp_dir = "tmp"[build]# Just plain old shell command. You could use `make` as well.cmd = "go build -o ./tmp/main ."# Binary file yields from `cmd`.bin = "tmp/main"# Customize binary.full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"# Watch these filename extensions.include_ext = ["go", "tpl", "tmpl", "html"]# Ignore these filename extensions or directories.exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]# Watch these directories if you specified.include_dir = []# Exclude files.exclude_file = []# Exclude specific regular expressions.exclude_regex = []# Exclude unchanged files.exclude_unchanged = true# Follow symlink for directoriesfollow_symlink = true# This log file places in your tmp_dir.log = "air.log"# It's not necessary to trigger build each time file changes if it's too frequent.delay = 1000 # ms# Stop running old binary when build errors occur.stop_on_error = true# Send Interrupt signal before killing process (windows does not support this feature)send_interrupt = false# Delay after sending Interrupt signalkill_delay = 500 # ms[log]# Show log timetime = false[color]# Customize each part's color. If no color found, use the raw app log.main = "magenta"watcher = "cyan"build = "yellow"runner = "green"[misc]# Delete tmp directory on exitclean_on_exit = true

这个 toml 格式蛾配置文件,放置在项目的根目录下。配置项包括:

root:根目录路径tmp_dir:临时文件路径build:编译命令,包括命令、日志、重启等额相关配置log:日志输出配置color:日志颜色配置misc:其他杂项

其中最为重要的是 build 编译命令的配置,包括子配置项:

cmd:编译的 shell 命令bin:编译得到的二进制输出的路径,一般放在临时路径full_bin:包括环境变量等的自定义二进制文件include_ext:需要监听的文件的文件扩展exclude_dir:需要排除监听的文件夹include_dir:需要监听的文件夹,默认全部exclude_file:排除监听的特定文件exclude_regex:排除监听的文件的正则模式exclude_unchanged:是否排除未变化的文件follow_symlink:是否跟随文件链接delay:触发重新编译的延迟时间,防止频繁修改带来的过于频繁的编译stop_on_error:是否在编译出错后停止log:日志输出文件send_interrupt:是否在项进程发送 kill 之前发送 interrupt 信号kill_delay:发送 interrupt 后到发送 kill 之间的的延迟时间

可以看到,air 提供了十分丰富的热加载配置,可以根据个人开发习惯和项目特性进行个性化配置,以适应不同的开发使用需求。

总结

Air 通过监听代码文件或目录的变化,进行自动编译和重启程序,大大提高了 Go 程序的开发效率。Air 提供了颜色丰富的日志输出,能够自定义编译命令,支持把特定的文件夹剔除出监听目标,支持监听在 air 启动后再新建的目录,提供了更好的编译过程。

默认的配置下,air 提供了足够好的热重载配置,直接替换 go run 命令为 air 命令就能实现热重载。同时,air 也以配置文件的形式提供了更为复杂和强大的配置功能,使其能够满足各种场景下的需求,十分灵活可扩展。

go air

以上内容为【Air - 提高开发效率的 Go 语言热重载工具(air工具)】的相关内容,更多相关内容关注中国智能工业网