当前位置:网站首页>【Golang】怎么实现Go程序的实时热更新
【Golang】怎么实现Go程序的实时热更新
2022-06-23 19:42:00 【DDGarfield】
在前端项目中,在开发中,通过npm启动devServer后,任何即时的代码修改,都会重启devServer,就算是node.js后端项目,也有nodemon监听代码文件的变动,当代码改变之后,便会自动重启;这样大大方便了我们开发人员,提高了开发效率。那么Go中是否有如此方便的工具?因为在实际开发过程中,确实有很多这种情况需要重新运行,比如:
web开发中,针对前端请求修改request参数结构体字段的tag值;- 测试时,报错,但是忘记加日志,导致开发人员并不能准确定位错误;
Ctrl+C与go build 然后运行的切换,还是比较浪费时间的。好了,铺垫很久了,没错Go中也有类似的工具:Air
1.安装
go get -u github.com/cosmtrek/air
安装成功后,air可执行文件便和其他一些开发工具在/bin目录下,为了使用方便,请把air设置为环境变量,如果您还没有设置的话,当然windows下,配置环境变量时,习惯把go/bin也配置了。
2.添加配置文件
.air.conf
#创建.air.conf文件
new-item .air.conf #powershell命令
air命令首先在当前目录下查找 .air.conf配置文件,如果找不到就使用默认的。
编辑文件
# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
# 工作目录
# 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
root = "."
tmp_dir = "tmp"
[build]
# 只需要写你平常编译使用的shell命令。你也可以使用 `make`
# Windows平台示例: cmd = "go build -o tmp\main.exe ."
# cmd = "go build -o ./tmp/main ."
# 由`cmd`命令得到的二进制文件名
# Windows平台示例:bin = "tmp\main.exe"
# bin = "tmp/main"
cmd = "go build -o ./tmp/main.exe ."
bin = "tmp/main.exe"
# 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
# Windows平台示例:full_bin = "tmp\main.exe"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# 监听以下文件扩展名的文件.
include_ext = ["go", "tpl", "tmpl", "html"]
# 忽略这些文件扩展名或目录
exclude_dir = ["assets", "tmp", "vendor", "client/node_modules"]
# 监听以下指定目录的文件
include_dir = []
# 排除以下文件
exclude_file = []
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
delay = 1000 # ms
# 发生构建错误时,停止运行旧的二进制文件。
stop_on_error = true
# air的日志文件名,该日志文件放置在你的`tmp_dir`中
log = "air_errors.log"
[log]
# 显示日志时间
time = true
[color]
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# 退出时删除tmp目录
clean_on_exit = true
完整的air_example.conf示例配置如上,可以根据自己的需要修改。
3.执行命令
#当前目录执行air命令
air
4.写在最后
在windows下是有问题的,首先是windows下似乎无法像linux那样捕获信号,所以下面的feature不被windows支持。
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = true
kill_delay = 500 # ms
其次更改文件时,原先的旧进程并没有被杀死,web应用导致重启会报端口问题。
https://github.com/cosmtrek/air/issues/147
虽然在README.md中作者写道:P.S. Great thanks mattn's PR for supporting Windows platform.但是在这个pull request:https://github.com/cosmtrek/air/pull/1中的截图,明显看出跟博主遇到一样的问题,但是作者依然合并了,不够严谨呀。
抽个时间研究下源码,修复一下这个问题吧。windows下的童鞋如果不能马上切换开发环境,可以暂时放弃这个工具,做个了解,Linux或者Mac童鞋可以上手。
------------------- End -------------------
边栏推荐
- MySQL时间函数的运用,简单问题
- What are the useful personnel management software? Personnel management system software ranking!
- 80% of people will be wrong about the three counter intuitive questions?
- Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
- 金鱼哥RHCA回忆录:DO447管理用户和团队的访问--用团队有效地管理用户
- Virtp4 notes
- Vprom notes
- Timertasks notes
- Is it safe to make new debt
- RStudio 1.4软件安装包和安装教程
猜你喜欢

JS高级程序设计第 4 版:生成器的学习

Application de JDBC dans les essais de performance

如何使用物联网低代码平台进行流程管理?

SAVE: 软件分析验证和测试平台

LeetCode 260. Number III that appears only once

Chaos engineering, learn about it

35歲危機?內卷成程序員代名詞了…

LeetCode 260. 只出现一次的数字 III

Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation

FlagAI飞智:AI基础模型开源项目,支持一键调用OPT等模型
随机推荐
深入理解和把握数字经济的基本特征
LeetCode 1079. movable-type printing
Implementation of microblog system based on SSM
解读2022年度敏捷教练行业现状报告
What are the useful personnel management software? Personnel management system software ranking!
Syntax of SQL union query (inline, left, right, and full)
35 year old crisis? It has become a synonym for programmers
Design of hardware switch with programmable full function rate limiter
Shell Scripting
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
I came from a major, so I didn't want to outsource
Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
Application of JDBC in performance test
Save: software analysis, verification and test platform
@@Script implementation of ishell automatic deployment
JDBC 在性能测试中的应用
准备好迁移上云了?请收下这份迁移步骤清单
Robust extraction of specific signals with time structure (Part 2)
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)