前言
我们在使用gin
开发web项目的时候,传统的运行方式:使用go run app.go
去运行自己的项目。但是在开发过程中,每改一次代码,都要进行一次重启,那在go 中如何使用热加载呢?
什么是热加载
热加载在开发过程中,改动代码或新增代码时自动加载更新内容并重新发布。热加载有以下用处:
- 自动加载更新内容
- 自动发布更新内容
- 提高开发效率,专注业务开发,而不需要反复重启应用进行调试接口
安装fresh
Fresh 是一个命令行工具,它会在您每次保存 Go 或模板文件时构建和(重新)启动您的 Web 应用程序。
如果您使用的 Web 框架支持 Fresh runner,它会在您的浏览器上显示构建错误。
传送门:fresh github
安装fresh
前置条件:必须有go环境。
查看go 版本:
go version
go version go1.18.1 darwin/amd64
安装 fresh
go get github.com/pilu/fresh
使用fresh 进行热加载
在传统的开发中,运行go 程序,是通过go run main.go
命令运行的,使用了fresh
之后,运行方式有所变更,直接命令 fresh
就可以了。
chatgin % fresh
22:33:52 runner | InitFolders
22:33:52 runner | mkdir ./tmp
22:33:52 runner | mkdir ./tmp: file exists
22:33:52 watcher | Watching .
22:33:52 watcher | Watching evn
22:33:52 watcher | Watching src
22:33:52 watcher | Watching src/authenticate
22:33:52 watcher | Watching src/common
22:33:52 watcher | Watching src/config
22:33:52 watcher | Watching src/interfaces
22:33:52 watcher | Watching src/module
22:33:52 watcher | Watching src/router
22:33:52 watcher | Watching src/service
22:33:52 watcher | Watching src/tables
22:33:52 watcher | Watching src/utils
22:33:52 main | Waiting (loop 1)...
22:33:52 main | receiving first event /
22:33:52 main | sleeping for 600 milliseconds
22:33:53 main | flushing events
22:33:53 main | Started! (38 Goroutines)
22:33:53 main | remove tmp/runner-build-errors.log: no such file or directory
22:33:53 build | Building...
22:33:53 runner | Running...
22:33:53 main | --------------------
22:33:53 main | Waiting (loop 2)...
22:33:53 app | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[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)
22:33:53 app | [GIN-debug] POST /auth/login --> org.chatgin/src/router.Router.func1 (5 handlers)
22:33:53 app | [GIN-debug] POST /user/register --> org.chatgin/src/router.Router.func2 (5 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] Listening and serving HTTP on :8080
问题处理
安装错误
在Mac 上安装fresh 时,发生以下错误:
xxx@MacBook-Pro ~ % go get github.com/pilu/fresh
package golang.org/x/sys/unix: unrecognized import path "golang.org/x/sys/unix": https fetch: Get "https://golang.org/x/sys/unix?go-get=1": dial tcp 142.251.43.17:443: i/o timeout
当出现上面的问题时,按提示要求先import golang.org/x/sys/unix
,如何在$GOPATH
下导入这个依赖呢?
# 进入$GOPATH目录
cd $GOPATH/src
# 创建 golang.org/x 目录
mkdir -p golang.org/x
# 进入 golang.org/x
cd golang.org/x
# 下载 源码
git clone https://github.com/golang/sys.git
等待下载完成,就完成了golang.org/x/sys/unix
的导入,然后在$GOPATH
重新运行go get github.com/pilu/fresh
就可以了。
fresh 运行:command not found fresh
设置环境变量:
# 修改环境变量
vim ~/.zshrc
# add config
export GOROOT=/usr/local/go
export GOPATH=/Users/xxxx/xxxx/go_workspace
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 让设置生效
source ~/.zshrc
网友评论