go 项目监听重启
Posted huay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go 项目监听重启相关的知识,希望对你有一定的参考价值。
脚本
/** 自动重启gin框架文件 执行命令 go 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 }
以上是关于go 项目监听重启的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础
如何从该片段中的 onItemSelectedListener 中获取微调器单击的项目?
解决go: go.mod file not found in current directory or any parent directory; see ‘go help modules‘(代码片段