如何在Go中使用Gin框架和Fresh实现热更新并监控整个项目目录?

go用gin框架 实现热更新fresh
加入目录结构是/myapp/cmd/server/main.go
那我需要进入到server 执行fresh 但是fresh只能监控到当前执行的目录也就是server和他的子目录
我应该如何才能在myapp下面执行呢 这样能监控所有

测试

阅读 2k
2 个回答

使用 Fresh 在 Go 项目中实现热更新

目标

/myapp 根目录下运行 fresh,同时监控整个项目(包括 /myapp/cmd/server/main.go 及其它子目录)。

1. 理解 Fresh 的工作原理

  • fresh 是 Go 的热重载工具,监控 .go 文件变化后自动重新编译和运行程序。
  • 默认行为fresh 仅监控执行命令时的当前目录及其子目录。
  • 问题:在 /myapp/cmd/server 目录下运行 fresh 时,无法监控 /myapp 根目录及其它平行目录(如 /myapp/pkg/myapp/internal)。

2. 目录结构

假设你的项目结构如下:

/myapp
├── cmd
│   └── server
│       └── main.go
├── internal
│   └── somepackage
│       └── somefile.go
├── pkg
│   └── anotherpackage
│       └── anotherfile.go
├── go.mod
└── fresh.conf

3. 解决方案

步骤 1:安装 Fresh

确定已安装 fresh

go install github.com/pilu/fresh@latest

步骤 2:创建 Fresh 配置文件

/myapp 目录下创建 fresh.conf 文件:

root: .
tmp_path: ./tmp
build_name: myapp
build_log: ./tmp/build.log
build_delay: 200
build_target: ./cmd/server
runner: ./tmp/myapp
watch:
  - ./**/*.go
exclude:
  - vendor
  - tmp

配置说明

  • root: . → 设定项目根目录为 /myapp
  • build_target: ./cmd/server → 构建目标为 /myapp/cmd/server/main.go
  • watch: - ./**/*.go → 监控整个项目的 .go 文件。

步骤 3:运行 Fresh

/myapp 目录下执行:

fresh

此时 fresh 会:

  • 监控 /myapp 下所有 .go 文件(包括 cmd, internal, pkg 等)。
  • 编译 /myapp/cmd/server/main.go 并生成可执行文件 /myapp/tmp/myapp
  • 运行应用,在文件变化时自动重新编译和重启。

4. 替代方法:命令行参数

如果不想使用配置文件,可以直接在 /myapp 目录执行:

fresh -c fresh.yml -w . -b ./cmd/server

参数解析

  • -w . → 监控 /myapp 目录及子目录。
  • -b ./cmd/server → 构建目标为 /myapp/cmd/server

5. 运行测试

  1. /myapp 目录下执行:

    fresh
  2. 访问 http://localhost:8080/ping,应该返回:

    {"message": "pong"}
  3. 修改 main.go 或其它 .go 文件(例如添加新路由)。
  4. fresh 发现文件变更后自动重启应用。
  5. 重新访问 http://localhost:8080/ping,确认更新生效。

6. 常见问题

🔹 Q:fresh 没有监控某些目录?
A:检查 fresh.conf 中的 watch 配置,建议使用:

watch:
  - ./**/*.go

🔹 Q:编译失败?
A:查看 /myapp/tmp/build.log,检查是否有语法错误或依赖问题。

🔹 Q:fresh 没有自动重启?
A:保证修改的文件在 watch 目录中,且未被 exclude 排除。


使用 Air 实现 Go 项目热更新

✅ 第一步:安装 Air

使用以下命令安装 Air:

go install github.com/cosmtrek/air@latest

确保 $GOPATH/bin 已加入你的环境变量,这样你就可以直接使用 air 命令。


✅ 第二步:创建配置文件 .air.toml(可选但推荐)

在你的项目根目录下创建 .air.toml 文件,示例如下:

# .air.toml
root = "."
tmp_dir = "tmp"

[build]
  cmd = "go build -o ./tmp/main"
  bin = "tmp/main"
  include_ext = ["go", "tpl", "tmpl", "html"]
  exclude_dir = ["vendor", "tmp", "node_modules"]
  exclude_file = []
  delay = 1000
  stop_on_error = true

[log]
  time = true

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

✅ 第三步:运行 Air

在项目根目录下运行:

air

Air 会自动监听 .go 文件的变更,重新编译并运行程序。


📁 示例项目结构

my-go-app/
├── main.go
├── go.mod
├── .air.toml
└── tmp/         # Air 会自动创建

🚀 示例 main.go

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, Air! 🚀")
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("Server running at http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

🧠 小贴士

  • 修改 main.go 后保存,Air 会自动重启程序
  • .air.toml 可根据项目结构灵活配置
  • 支持多模块项目、复杂构建命令等

Go 原生不支持,但后来提到的工具(如 Fresh、Air)是 社区开发的辅助工具,它们通过监听文件变更并自动重启程序,模拟出类似 Hot Reload 的开发体验

以下是 Fresh 和 Air 的对比表格:

特性FreshAir
🔧 安装方式go installgo install
🔁 热更新机制监听文件变更,重启程序同上,但更稳定
⚙️ 配置灵活性简单,适合小项目更强,适合中大型项目
📁 默认支持的文件类型.go, .html, .tpl可自定义
🧪 稳定性偶尔重启失败更稳定,社区活跃
📦 项目支持度适合简单 Web 项目支持复杂项目结构
🧰 配置文件runner.confair.toml(更强大)
🧑‍💻 社区维护较少更新活跃,持续维护中

✅ 推荐建议

  • 选择 Air 如果你的项目结构较复杂(如多模块、多目录),希望更稳定的热更新体验,或者需要更强的配置能力(如忽略某些目录、设置构建命令)。
  • 选择 Fresh 如果你的项目简单,需要快速上手,不想写复杂配置文件,或者只是临时开发调试用。

go没有hot refresh功能哦。
freshair都是live reload,就是监控文件改动然后帮你重新编译重新启动而已。

fresh已经很久没有更新了,建议使用air

air的配置文件air_example.toml

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进