Golang项目文件变更时Go-Watcher报错,如何自动重建

Golang项目文件变更时Go-Watcher报错,如何自动重建 大家好!我是Go语言的新手。(我是一名Angular和Node开发者)我开始喜欢上Go了,但我需要一个类似nodemon的工具,以便在文件编辑时重新构建并重启我的Go服务器。我的团队成员给我推荐了这个工具:

GitHub

GitHub - canthefason/go-watcher: 监控你的 .Go 文件并重启你的…

监控你的 .Go 文件并重启你的进程,无需任何配置麻烦 - GitHub - canthefason/go-watcher: 监控你的 .Go 文件并重启你的进程,无需任何配置麻烦

我必须在项目根目录下像这样运行它:

DEBUG=database GOPATH=/Users/yankoaleksandrov/go watcher -c config.dev.yaml

但我遇到了以下问题: 2020/10/15 18:42:07 构建开始 正在构建… 2020/10/15 18:42:08 构建完成 正在运行… 2020/10/15 18:42:08 进程被中断:信号:已终止

有没有更好的监控工具推荐,或者如何修复这个问题? 我使用的是 macOS Catalina。

我可以使用以下命令启动服务器:

make ./kamion_server -c config.dev.yaml

这样一切都能正常运行。


更多关于Golang项目文件变更时Go-Watcher报错,如何自动重建的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang项目文件变更时Go-Watcher报错,如何自动重建的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go项目中实现文件变更自动重建,推荐使用airfresh替代go-watcher。以下是具体解决方案:

1. 推荐使用 Air(当前最流行的热重载工具)

安装:

go install github.com/cosmtrek/air@latest

在项目根目录创建.air.toml配置文件:

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  cmd = "go build -o ./tmp/main ."
  bin = "tmp/main"
  include_ext = ["go", "tpl", "tmpl", "html"]
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  include_dir = []
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  log = "build-errors.log"
  delay = 1000
  stop_on_error = true
  send_interrupt = false
  kill_delay = 500

[log]
  time = false

[color]
  main = "magenta"
  watcher = "cyan"
  build = "yellow"
  runner = "green"

[misc]
  clean_on_exit = true

运行:

air -c config.dev.yaml

2. 使用 Fresh(轻量级替代方案)

安装:

go install github.com/gravityblast/fresh@latest

创建runner.conf

root:              .
tmp_path:          ./tmp
build_name:        runner-build
build_log:         runner-build-errors.log
valid_ext:         .go, .tpl, .tmpl, .html
build_delay:       1000
colors:            1
log_color_main:    cyan
log_color_build:   yellow
log_color_runner:  green
log_color_watcher: magenta
log_color_app:     

运行:

fresh -c config.dev.yaml

3. 直接使用内置的go run配合文件监控

创建简单的监控脚本watch.sh

#!/bin/bash
while true; do
    go run main.go -c config.dev.yaml
    echo "程序重启于 $(date)"
    sleep 2
done

4. 使用 nodemon(如果你熟悉Node.js环境)

安装nodemon:

npm install -g nodemon

创建nodemon.json

{
  "watch": ["."],
  "ext": "go",
  "ignore": ["vendor", "tmp"],
  "exec": "go run main.go -c config.dev.yaml",
  "delay": "1000"
}

运行:

nodemon

5. 针对你的Makefile集成

修改Makefile添加监控目标:

.PHONY: watch
watch:
    air -c config.dev.yaml

.PHONY: dev
dev:
    go build -o kamion_server .
    ./kamion_server -c config.dev.yaml

.PHONY: run
run:
    go run main.go -c config.dev.yaml

然后运行:

make watch

Air会自动处理环境变量传递,无需手动设置GOPATHDEBUG。如果仍有进程终止问题,检查程序是否正确处理了SIGINT信号:

package main

import (
    "context"
    "os"
    "os/signal"
    "syscall"
)

func main() {
    ctx, stop := signal.NotifyContext(context.Background(), 
        os.Interrupt, syscall.SIGTERM)
    defer stop()
    
    // 你的服务器代码
    
    <-ctx.Done()
    // 清理资源
}

建议优先使用Air,它功能完善且社区活跃,能有效解决go-watcher的兼容性问题。

回到顶部