监听文件变化并重启
方式一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** 自动重启gin框架文件 执行命令 go run example.go -start main.go **/ package main import ( "flag" "fmt" "github.com/fsnotify/fsnotify" "log" "os" "os/exec" "path/filepath" "runtime" "strings" "time" ) func buildServer(target string) string { binPath, err1 := exec.LookPath("go") if err1 != nil { fmt.Println("Cannot find go executable file", err1) os.Exit(-2) } outputName := "" if target == "." { outputName = "project.bin" } else if strings.HasSuffix(target, ".go") { if runtime.GOOS == "windows" { outputName = strings.Split(target, ".")[0] + ".exe" } else { outputName = strings.Split(target, ".")[0] } } else { fmt.Println("taget must: '.' or '*.go' ", err1) os.Exit(-2) } // pwd, _ := os.Getwd() args := []string{"go", "build", "-o", outputName, target} procAttr := &os.ProcAttr{ Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, } process, err := os.StartProcess(binPath, args, procAttr) if err != nil { fmt.Println(err) } fmt.Println(args, "executing") process.Wait() fmt.Printf("Build %s success ", outputName) return outputName } func createProcess(target string) (*os.Process, string) { outputName := buildServer(target) binPath, err1 := exec.LookPath("./" + outputName) if err1 != nil { fmt.Printf("Cannot find %s executable file ", outputName) os.Exit(-3) } // pwd, _ := os.Getwd() args := []string{binPath} procAttr := &os.ProcAttr{ Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, } process, err := os.StartProcess(outputName, args, procAttr) if err != nil { fmt.Println(err) os.Exit(-4) } return process, outputName } func main() { target := flag.String("start", ".", "go build && run it") flag.Parse() process, outputName := createProcess(*target) fmt.Printf("Running %s PID: %d ", outputName, process.Pid) watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } defer watcher.Close() done := make(chan bool) lastModify := time.Now().Unix() go func() { for { select { case event, ok := <-watcher.Events: if !ok { return } log.Println("event:", event) if event.Op&fsnotify.Write == fsnotify.Write { if strings.HasSuffix(event.Name, ".go") { if time.Now().Unix()-lastModify > 3 { lastModify = time.Now().Unix() err := process.Kill() if err != nil { fmt.Println(err) os.Exit(-1) } else { fmt.Println("Restarting...") process, outputName = createProcess(*target) } } else { fmt.Println("Update too full, ignored", process.Pid) } } } case err, ok := <-watcher.Errors: if !ok { return } log.Println("error:", err) } } }() e := filepath.Walk("./", func(path string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { err = watcher.Add(path) log.Printf("Dir:%s add to watch", path) if err != nil { log.Fatal(err) } } return nil }) if e != nil { fmt.Printf("filepath.Walk() returned %v ", err) } <-done }
方式二
安装air
go get -u github.com/cosmtrek/air
配置文件.air.conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件 # 工作目录 # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下 root = "." tmp_dir = "tmp" [build] # 只需要写你平常编译使用的shell命令。你也可以使用 `make` cmd = "go build ./main.go" # 由`cmd`命令得到的二进制文件名 bin = "./main.exe" # 自定义的二进制,可以添加额外的编译标识例如添加 GIN_MODE=release #full_bin = "APP_ENV=dev APP_USER=air ./tmp/main" # 监听以下文件扩展名的文件. include_ext = ["go"] # 忽略这些文件扩展名或目录 exclude_dir = ["log",".idea",".git"] # 监听以下指定目录的文件 include_dir = [] # 排除以下文件 exclude_file = [] # 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间 delay = 0 # ms # 发生构建错误时,停止运行旧的二进制文件。 stop_on_error = true # air的日志文件名,该日志文件放置在你的`tmp_dir`中 log = "./log/air_errors.log" [log] # 显示日志时间 time = true [color] # 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。 main = "magenta" watcher = "cyan" build = "yellow" runner = "green" [misc] # 退出时删除tmp目录 clean_on_exit = true
通过air命令即可执行