当前位置:网站首页>[golang] how to realize real-time hot update of Go program
[golang] how to realize real-time hot update of Go program
2022-06-23 20:05:00 【DDGarfield】
In front-end projects , In development , adopt npm start-up devServer after , Any immediate code changes , Will restart devServer, Even if it is node.js The back-end project , Also have nodemon Listen for code file changes , When the code changes , It will restart automatically ; This greatly facilitates our developers , Improved development efficiency . that Go Is there such a convenient tool in ? Because in the actual development process , There are indeed many such cases that need to be re run , such as :
webIn development , Modify for the front-end requestrequestParameter structure fieldtagvalue ;- When testing , Report errors , But forget to add a log , As a result, developers can not accurately locate errors ;
Ctrl+C And go build Then run the switch , It's a waste of time . Okay , For a long time , you 're right Go There are similar tools in :Air
1. install
go get -u github.com/cosmtrek/air
After successful installation ,air Executable files and other development tools are used in /bin Under the table of contents , For ease of use , Please put air Set to environment variable , If you haven't set it yet , Of course windows Next , When configuring environment variables , Get used to go/bin Also configured the .
2. Add configuration file
.air.conf
# establish .air.conf file
new-item .air.conf #powershell command
air The command first looks in the current directory .air.conf The configuration file , If you can't find it, use the default .
Edit the file
# [Air](https://github.com/cosmtrek/air) TOML Format of the configuration file
# working directory
# Use . Or absolute path , Please note that `tmp_dir` The directory must be in `root` Under the table of contents
root = "."
tmp_dir = "tmp"
[build]
# Just write what you usually compile and use shell command . You can also use `make`
# Windows Platform examples : cmd = "go build -o tmp\main.exe ."
# cmd = "go build -o ./tmp/main ."
# from `cmd` Binary file name obtained by command
# Windows Platform examples :bin = "tmp\main.exe"
# bin = "tmp/main"
cmd = "go build -o ./tmp/main.exe ."
bin = "tmp/main.exe"
# Custom commands for executing programs , Additional compilation identifiers can be added, such as GIN_MODE=release
# Windows Platform examples :full_bin = "tmp\main.exe"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Listen for files with the following file extensions .
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these file extensions or directories
exclude_dir = ["assets", "tmp", "vendor", "client/node_modules"]
# Listen for files in the following specified directory
include_dir = []
# Exclude the following files
exclude_file = []
# If the file changes too often , There is no need to trigger a build every time a change is made . You can set the delay time to trigger the build
delay = 1000 # ms
# When a build error occurs , Stop running old binaries .
stop_on_error = true
# air The log file name of , The log file is placed in your `tmp_dir` in
log = "air_errors.log"
[log]
# Show log time
time = true
[color]
# Customize the color of each part . If you can't find the color , Use the original application log .
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# Delete on exit tmp Catalog
clean_on_exit = true
complete air_example.conf The sample configuration is as above , It can be modified according to your own needs .
3. Carry out orders
# Current directory execution air command
air
4. At the end
stay windows There's something wrong with it , First of all windows It doesn't seem like linux That's how the signal is captured , So the following feature Don't be windows Support .
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = true
kill_delay = 500 # ms
Second, when changing files , The original old process was not killed ,web Port problems will be reported when the application is restarted .
https://github.com/cosmtrek/air/issues/147
Although in README.md The author wrote :P.S. Great thanks mattn's PR for supporting Windows platform. But here pull request:https://github.com/cosmtrek/air/pull/1 In the screenshot , Obviously, it is the same problem as the blogger , But the author still merged , Not rigorous enough .
Take time to study the source code , Fix this problem .windows If you can't switch the development environment immediately , You can give up this tool for a while , Let's get to know each other ,Linux perhaps Mac Children's shoes can be used .
------------------- End -------------------
边栏推荐
- Application of JDBC in performance test
- 怎么开户?在国海证券开户安全吗?需要带什么?
- Kinsoku jikou desu新浪股票接口变动
- Application de JDBC dans les essais de performance
- 【白话技术】二维码
- The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
- I came from a major, so I didn't want to outsource
- LeetCode 1079. movable-type printing
- JS高级程序设计第 4 版:生成器的学习
- Activity registration | introduction to mongodb 5.0 sequential storage features
猜你喜欢

JDBC 在性能测试中的应用

Daily question brushing record (II)

Kubernetes resource topology aware scheduling optimization

The golden nine silver ten, depends on this detail, the offer obtains the soft hand!

ZABBIX monitoring - Aruba AP operation data

Ready to migrate to the cloud? Please accept this list of migration steps

盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3

Open source SPL redefines OLAP server

直播分享| 腾讯云 MongoDB 智能诊断及性能优化实践

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...
随机推荐
LeetCode 260. 只出现一次的数字 III
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
SQL聯合查詢(內聯、左聯、右聯、全聯)的語法
开源 SPL 重新定义 OLAP Server
混沌工程,了解一下
Save: software analysis, verification and test platform
SAVE: 软件分析验证和测试平台
How to use data warehouse to create time series
【Golang】快速复习指南QuickReview(一)——字符串string
【Golang】怎样优雅的清空切片
Want to open an account, is it safe to open an account on the stock Internet? Will the funds be cheated away?
每日刷题记录 (二)
国元期货交易软件正规吗?如何安全下载?
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
UGeek大咖说 | 可观测之超融合存储系统的应用与设计
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
35 year old crisis? It has become a synonym for programmers
Stochastic process -- Markov chain
Emmet语法规范
@@Script implementation of ishell automatic deployment