使用gin搭建服务的时候,在开发环境修改代码,要关闭服务,再重新打开代码才能生效。
频繁修改再打开是一个很低效的事情。所以需要一个工具,在修改完代码之后可以热启动。
可以使用 air来完成。
安装
go install github.com/cosmtrek/air@latest
配置
air
不会自动配置路径,需要我们添加
alias air='$(go env GOPATH)/bin/air'
然后添加一个air的配置文件
root = "."testdata_dir = "testdata"tmp_dir = "tmp"[build]args_bin = []bin = "./tmp/main"cmd = "go build -o ./tmp/main ."delay = 1000exclude_dir = ["assets", "tmp", "vendor", "testdata"]exclude_file = []exclude_regex = ["_test.go"]exclude_unchanged = falsefollow_symlink = falsefull_bin = ""include_dir = []include_ext = ["go", "tpl", "tmpl", "html"]kill_delay = "0s"log = "build-errors.log"send_interrupt = falsestop_on_error = true[color]app = ""build = "yellow"main = "magenta"runner = "green"watcher = "cyan"[log]time = false[misc]clean_on_exit = false[screen]clear_on_rebuild = false
执行命令
air
即可
修改bash zsh
vim ~/.zshrc # or we can # vim ~/.bash_profile# Add this in the filealias air='$(go env GOPATH)/bin/air'
这样以后打开的新的终端都可以应用了