许多人希望在写go时拥有实时加载代码(热编译)的效果,特别是那些习惯使用 javascript,python和ruby等解释语言的人,本文介绍了5种实时重新加载go程序的方法。
本文假设已安装go编译器,并且已将gopath/bin路径添加到path环境变量中。
在开始之前,我们先创建一个简单的web服务器,可以返回响应内容”hello,world”。
package mainimport ( net/http)func main() { http.handlefunc(/, func (w http.responsewriter, r *http.request) { w.write([]byte(hello, world)) }) http.listenandserve(:5000, nil)}
method 1: 使用airair 是一个命令行程序,可以为go程序提供实时代码加载。
通过运行以下命令来安装air。
go get -u github.com/cosmtrek/air
下一步,在使用项目的根目录中创建air配置文件.air.conf。
# .air.conf# toml配置文件来源于 [air](https://github.com/cosmtrek/air)# 工作区间# .(当前目录)或绝对路径, 注意这些目录都在根目录下面.root = . tmp_dir = tmp[build]# 只是普通的shell命令。 可以使用`make`。cmd = go build -o ./tmp/main .# `cmd`配置命令输出的二进制文件的位置。bin = tmp/main# 自定义二进制输出。full_bin = app_env=dev app_user=air ./tmp/main# 监听的文件扩展后缀列表。include_ext = [go, tpl, tmpl, html]# 忽略这些文件扩展名或目录。exclude_dir = [assets, tmp, vendor, frontend/node_modules]# 如果指定,则监听包含这些文件。include_dir = []# 忽略文件列表.exclude_file = []# 如果文件修改太频繁,则不必在每次修改时都立刻触发构建,设置触发等待时间。delay = 1000 # ms# 发生编译错误时,是否停止旧的二进制程序。stop_on_error = true# 该日志文件放置在tmp_dir中。log = air_errors.log[log]# 日志是否显示时间time = false[color]# 自定义每类输出的颜色。 如果找不到这个颜色,使用原本的日志输出演示。main = magentawatcher = cyanbuild = yellowrunner = green[misc]# 退出时是否删除临时目录clean_on_exit = true
配置是简单明了的,请根据你的项目情况去调整。
最后,不要使用常用的go run命令来运行go程序,而应使用air命令来启动程序。
method 2: docker运行ari这种方法需要使用docker,如果你没有安装,可以按照
我们仍将使用air库,因此仍然需要air配置文件。 如果你还没有,请创建一个配置文件。
docker镜像cosmtrek/air附带安装了air命令,并且gopath环境变量设置为/go。
我们只需要将我们的项目目录挂载到docker容器的gopath中,并使用-p暴露需要使用的端口即可。 我们可以通过运行docker run命令来实现这一点:
docker run -it --rm -w <working_dir> -v <project_folder>:<mount_point> -p <host_port>:<container_port> <image_name>
就我而言,我需要运行以下命令:
docker run -it --rm -w /go/src/github.com/praveen001/live-reloading -v /go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading -p 5000:5000 cosmtrek/air
解释:
使用-v参数将项目目录 /home/praveen/go/src/github.com/praveen001/live-reloading 挂载到容器里面的gopath中的目录/go/src/github.com/praveen001/live-reloading。
-v /home/praveen/go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading
使用-w参数指定挂载目录成工作目录。
-w /go/src/github.com/praveen001/live-reloading
web服务器正在监听端口5000,因此需要使用-p标志将容器端口5000暴露到主机端口5000。
-p 5000:5000
最后,指定docker镜像名称cosmtrek / air。
method 3: 使用gingin是另一个用于实时重新加载go应用程序的命令行程序。
通过运行以下命令来安装gin。
go get github.com/codegangsta/gin
而不是使用通常的go run main.go命令运行应用程序,而是使用gin命令。
就我而言,--appport参数告诉gin监听端口5000,--port参数告诉gin代理监听端口3000端口
gin --appport 5000 --port 3000
现在使用地址http://localhost:3000访问gin程序.
如果要排除监听那个目录可以使用--excludedir参数,例如:
gin --appport 5000 --port 3000 --excludedir ./frontend
如果你项使用gin实现加载没有启动端口监听的程序,你们必须使用--immediate参数。但是gin仍然会去5000端口。
你可以在这找到所有受支持的参数gin的github.
method 4: 使用nodemonnodemon是另一个用于实时重新加载node应用程序的命令行程序。 但是可以通过使用--exec参数设置启动命令用于启动其他应用程序。
nodemon需要安装nodejs和npm。 如果没有安装,可以按照nodejs的官方文档进行安装.
运行以下命令来安装nodemon:
npm install -g nodemon
现在,我们可以通过运行以下命令来使用nodemon运行web服务器:
nodemon --exec go run main.go --signal sigterm
如果要配置nodemon,请在项目的根目录中创建配置文件nodemon.json。 完整可用的示例配置文件
method 5: 使用freshfresh 是另一个go实现的用于实时重新加载go的程序
安装fresh
go get github.com/pilu/fresh
而不是使用常用的go run main.go命令来运行应用程序,而是使用fresh命令。
fresh
要配置fresh,需要在项目的根目录中创建一个配置文件runner.conf。
这是一个示例配置文件。
root: .tmp_path: ./tmpbuild_name: runner-buildbuild_log: runner-build-errors.logvalid_ext: .go, .tpl, .tmpl, .htmlno_rebuild_ext: .tpl, .tmpl, .htmlignored: assets, tmpbuild_delay: 600colors: 1log_color_main: cyanlog_color_build: yellowlog_color_runner: greenlog_color_watcher: magentalog_color_app:
总结还有许多其他工具,例如:
facebook’s watchmanrealizereflexeven a custom–built solution原文地址:https://techinscribed.com/5-ways-to-live-reloading-go-applications/
译文地址:https://learnku.com/go/t/51189
以上就是分享5种文件变更时自动重载go程序的方法的详细内容。
