1. 安装fresh工具

Fresh是一个命令行工具,每次更新代码都能够自动重新加载并执行,这在开发过程中非常便利的,省去了每次手动重新编译的麻烦。

PS D:\devops\go\src\github.com\lamxops\gogin> go install github.com/pilu/fresh@latest go: downloading github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 go: finding module for package github.com/pilu/config go: finding module for package github.com/howeyc/fsnotify go: finding module for package github.com/mattn/go-colorable go: downloading github.com/mattn/go-colorable v0.1.13 go: downloading github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a go: downloading github.com/howeyc/fsnotify v0.9.0 go: found github.com/howeyc/fsnotify in github.com/howeyc/fsnotify v0.9.0 go: found github.com/mattn/go-colorable in github.com/mattn/go-colorable v0.1.13 go: found github.com/pilu/config in github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a 2. 使用fresh

在gin项目目录下执行fresh命令即可,替代原来的go run命令。

PS D:\devops\go\src\github.com\lamxops\gogin> fresh 15:0:56 runner | InitFolders 15:0:56 runner | mkdir ./tmp 15:0:56 watcher | Watching . 15:0:56 main | Waiting (loop 1)... 15:0:56 main | receiving first event / 15:0:56 main | sleeping for 600 milliseconds 15:0:57 main | flushing events 15:0:57 main | Started! (5 Goroutines) 15:0:57 main | remove tmp\runner-build-errors.log: The system cannot find the file specified. 15:0:57 build | Building... 15:0:58 runner | Running... 15:0:59 main | -------------------- 15:0:59 main | Waiting (loop 2)... 15:0:59 app | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. 15:0:59 app | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /ping --> main.main.func1 (3 handlers) [GIN-debug] GET /hello --> main.main.func2 (3 handlers) [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details. [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080 15:1:10 app | [GIN] 2022/11/28 - 15:01:10 | 200 | 715.2µs | 127.0.0.1 | GET "/hello" 15:1:10 app | [GIN] 2022/11/28 - 15:01:10 | 404 | 0s | 127.0.0.1 | GET "/favicon.ico"

当代码文件发生变更时,fresh会检测到,并自动构建重启gin项目:

15:1:14 watcher | sending event ".\\example.go": MODIFY 15:1:14 main | receiving first event ".\\example.go": MODIFY 15:1:14 main | sleeping for 600 milliseconds 15:1:14 watcher | sending event ".\\example.go": MODIFY 15:1:14 main | flushing events 15:1:14 main | receiving event ".\\example.go": MODIFY 15:1:14 main | Started! (8 Goroutines) 15:1:14 main | remove tmp\runner-build-errors.log: The system cannot find the file specified. 15:1:14 build | Building... 15:1:16 runner | Running... 15:1:16 runner | Killing PID 21488 15:1:16 main | -------------------- 15:1:16 main | Waiting (loop 3)... 15:1:16 app | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. 15:1:16 app | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /ping --> main.main.func1 (3 handlers) [GIN-debug] GET /hello --> main.main.func2 (3 handlers) [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details. [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080 15:1:17 app | [GIN] 2022/11/28 - 15:01:17 | 200 | 0s | 127.0.0.1 | GET "/hello"