go:用第三方库使fresh使代码修改后自动热重启

一,安装fresh库

$ go get github.com/pilu/fresh

二,遇到问题:

执行fresh时报错:

$ fresh
fresh:未找到命令

解决:

$ go install github.com/pilu/fresh@latest

再次执行:

$ go get github.com/pilu/fresh

注意:此时fresh命令被安装到了$GOPATH/bin/fresh
给它添加符号链接:

# ln -s /data/gopath/bin/fresh /usr/bin/fresh

之后fresh命令可用

三,遇到问题之二:

$ fresh
17:18:43 runner      | InitFolders
17:18:43 runner      | mkdir ./tmp
17:18:43 watcher     | Watching .
17:18:43 watcher     | Watching config
17:18:43 watcher     | Watching controller
。。。
inotify_init: too many open files

解决:

提示需要监控的文件太多,把不需要的文件从项目目录下移除即可

 

四,测试效果

1,在项目目录下运行fresh命令

$ fresh  
17:28:28 runner      | InitFolders
17:28:28 runner      | mkdir ./tmp
17:28:28 runner      | mkdir ./tmp: file exists
17:28:28 watcher     | Watching .
17:28:28 watcher     | Watching config
17:28:28 watcher     | Watching controller
17:28:28 watcher     | Watching middleware
17:28:28 watcher     | Watching model
17:28:28 watcher     | Watching pkg
...
17:28:30 app         | 
17:28:30 app         |  ┌───────────────────────────────────────────────────┐ 
17:28:30 app         |  │                   
17:28:30 app         | Fiber v2.52.5
17:28:30 app         |                    │ 
17:28:30 app         |  │               http://127.0.0.1:3000               │ 
17:28:30 app         |  │       (bound on host 0.0.0.0 and port 3000)       │ 
17:28:30 app         |  │                                                   │ 
17:28:30 app         |  │ Handlers ........... 
17:28:30 app         | 151
17:28:30 app         |   Processes ........... 
17:28:30 app         | 1
17:28:30 app         |  │ 
17:28:30 app         |  │ Prefork ....... Disabled  PID .............. 
17:28:30 app         | 4975
17:28:30 app         |  │ 
17:28:30 app         |  └───────────────────────────────────────────────────┘
17:28:30 app         |  

此时应用已经启动

2,当我们修改go的源文件后,无需手动执行 go run main.go 去重启,
fresh会自动重启进程,如下:

17:31:50 watcher     | sending event "controller/articleController.go": MODIFY
17:31:50 main        | receiving first event "controller/articleController.go": MODIFY
17:31:50 main        | sleeping for 600 milliseconds
17:31:51 main        | flushing events
17:31:51 main        | Started! (209 Goroutines)
17:31:51 main        | remove tmp/runner-build-errors.log: no such file or directory
17:31:51 build       | Building...
17:31:53 runner      | Running...
17:31:53 main        | --------------------
17:31:53 main        | Waiting (loop 3)...
17:31:53 runner      | Killing PID 4975
17:31:53 app         | setting:

重新访问我们程序的接口,会发现修改已经生效

 

posted @ 2025-01-18 16:51  刘宏缔的架构森林  阅读(343)  评论(0)    收藏  举报