下面由golang教程栏目给大家介绍如何使用air自动重载代码,希望对需要的朋友有所帮助!

Air能够实时监听项目的代码,在代码发生更变之后自动重新编译并执行
安装Air(windows)https://github.com/cosmtrek/air/releasesair.execurl -fLo air.exe https://git.io/windows_airair -v
air
运行如下代码:
package mainimport (
"fmt"
"net/http")func handlerFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>Air自动重载<h1>")}func main(){
http.HandleFunc("/", handlerFunc)
http.ListenAndServe(":3030", nil)}浏览器中访问localhost:3030/ 显示

修改代码
package mainimport (
"fmt"
"net/http")func handlerFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>Air自动重载<h1>")}func main(){
http.HandleFunc("/", handlerFunc)
http.ListenAndServe(":3000", nil)}分别访问localhost:3030/ 、 localhost:3000/ ,效果如下:


使用命令查看文件状态:
$ git status

tmptmp.gitignoretmp.gitignore
此时,我们再使用命令查看文件状态就能发现,tmp目录被排除在外:

以上便是Air自动重载在GO项目中的使用。