目的
Hot Reload 方便修改程式後, 讓程式自動編譯並執行. 省去每次手動編譯與執行.
記錄一下目前自己使用過的三個套件Air, Fresh, Realize
1. Air (https://github.com/cosmtrek/air)
- 較推薦使用此套件
- 原先是使用 fresh, 但是遇到fresh不能在執行指令後面帶參數.
features
- Colorful log output
- Customize build or binary command
- Support excluding subdirectories
- Allow watching new directories after Air started
- Better building process
Installation
# binary will be $(go env GOPATH)/bin/aircurl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin# or install it into ./bin/curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -sair -v
Usage
# 初始化設定檔air init# 預設找 .air.toml 此設定檔air# 選擇要執行的設定檔air -c .air.toml# Debugair -d
sample config
我的設定檔
# 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", "css", "js", "env", "yaml"]# Ignore these filename extensions or directories.exclude_dir = ["assets", "tmp", "vendor", "node_modules", "build"]# Watch these directories if you specified.include_dir = []# Exclude files.exclude_file = []# Exclude unchanged files.exclude_unchanged = 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
2. Fresh (https://github.com/gravityblast/fresh)
- 目前看起來沒在更新了
- 沒法在指令後面帶入參數
- 如果go build返回錯誤, 會將記錄在tmp資料夾中
Installation
go get github.com/pilu/fresh
Usage
fresh# 選擇要執行的設定檔fresh -c other_runner.conf
sample config
root: .tmp_path: ./tmpbuild_name: runner-buildbuild_log: runner-build-errors.logvalid_ext: .go, .tpl, .tmpl, .htmlno_rebuild_ext: .tpl, .tmpl, .htmlignored: assets, tmpbuild_delay: 600colors: 1log_color_main: cyanlog_color_build: yellowlog_color_runner: greenlog_color_watcher: magentalog_color_app:
3. realize (https://github.com/oxequa/realize)
features
- High performance Live Reload.
- Manage multiple projects at the same time.
- Watch by custom extensions and paths.
- All Go commands supported.
- Switch between different Go builds.
- Custom env variables for project.
- Execute custom commands before and after a file changes or globally.
- Export logs and errors to an external file.
- Step-by-step project initialization.
- Redesigned panel that displays build errors, console outputs and warnings.
總結
目前較推薦使 air , fresh 遇到無法帶入參數, 且可設定的東西較少決了. realize 忘了之前是mod還是vendor無法安裝, 不知道解了沒.