前言
在 Web 開發(fā)中匈勋,熱重啟在很多地方都會(huì)用到,它能極大提升開發(fā)效率庆揩,在我們修改完代碼后保存俐东,就能自動(dòng)重啟運(yùn)行得到最新的代碼的運(yùn)行結(jié)果,比如前端開發(fā)的 HMR 就是一個(gè)典型的例子盾鳞。
Go 語言中的熱重啟
在 Go 語言開發(fā)中實(shí)現(xiàn)熱重啟犬性,推薦使用 air ,自 Go1.4 出來腾仅,官方就推薦使用 Go Modules 管理依賴,來進(jìn)行模塊化開發(fā)套利,那么我們就使用 Go Modules 構(gòu)建我們的項(xiàng)目(注意:我使用的版本是 Go1.3)推励。首先創(chuàng)建一個(gè)項(xiàng)目目錄如: go-restart 鹤耍,然后進(jìn)入目錄,在該目錄下執(zhí)行終端命令初始化 Go Modules:
go mod init github.com/startdusk/go-restart
注意 github.com/startdusk/go-restart 只是一個(gè)模塊名验辞,你可以把它命名成其他的命名
然后就會(huì)在目錄下生成一個(gè) go.mod 文件
然后我們安裝 air (注意:還是在當(dāng)前目錄下執(zhí)行)
go get -v -u github.com/cosmtrek/air
安裝完成后稿黄,在終端執(zhí)行 air ,有一下輸出即為安裝成功:
我們先創(chuàng)建一個(gè) main.go 文件跌造,寫點(diǎn)簡單代碼:
// main.go
package main
import "fmt"
func main() {
fmt.Println("air test")
}
到這里杆怕,我們還需要?jiǎng)?chuàng)建一個(gè) .air.conf 的配置文件(.air.conf 是默認(rèn)命名)】翘埃可以從 air 項(xiàng)目中的 air_example.conf 復(fù)制配置內(nèi)容陵珍,進(jìn)行一些修改。
有幾個(gè)要注意的地方:
- 1.[build] 下的 cmd 配置默認(rèn)是 go build -o ./tmp/main .违施,很可能你需要修改互纯,比我當(dāng)前的項(xiàng)目就必須修改為 go build -o ./tmp/main github.com/startdusk/go-restart/main.go
- 2.include_ext 和 exclude_dir 是配置避免監(jiān)控不必要的文件和目錄
- 3.如果該項(xiàng)目有版本管理如 git ,要把 tmp 目錄加入 .gitignore 中(因?yàn)檫\(yùn)行 air 會(huì)生成的文件放在 tmp 文件夾中)
- 4.如果出現(xiàn) watch error, open tool many files…磕蒲,說明要監(jiān)控的文件太多留潦,一方面確保修改了了 include_ext 和 exclude_dir,另一方面可以通過 ulimit -n 5000 調(diào)大進(jìn)程允許打開的文件數(shù)
那么修改后的 .air.conf 文件就變?yōu)榱耍?/p>
# 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 github.com/startdusk/go-restart/main.go"
# 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 = []
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop to run old binary when build errors occur.
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"
[log]
# Show log time
time = 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 exit
clean_on_exit = true
此時(shí)目錄結(jié)構(gòu)為:
好了辣往,到此我們可以在命令行運(yùn)行代碼:
air
然后輸出:
程序成功運(yùn)行兔院,并且 air 在監(jiān)聽文件的變化,那我們嘗試下改動(dòng)下代碼:
// main.go
package main
import "fmt"
func main() {
fmt.Println("air test")
fmt.Println("air listen")
}
然后控制臺(tái)輸出:
查看結(jié)果站削,air 成功監(jiān)聽到了文件的變化坊萝,并重新運(yùn)行了程序。
那么钻哩,上面的粉紅色的輸出一些異常屹堰,大家可以去看一下這個(gè) issue #53,windows 平臺(tái)會(huì)出現(xiàn)這個(gè)問題街氢,但不是很影響使用扯键。但這個(gè) air 目前還不能配合 VSCode 熱重啟加調(diào)試模式使用(如 nodejs 的 nodemon+VSCode 是可以實(shí)現(xiàn)熱重啟加調(diào)試模式啟動(dòng)的),有點(diǎn)遺憾珊肃。