当前位置:网站首页>[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 -------------------
边栏推荐
- 如何避免基因领域“黑天鹅”事件:一场预防性“召回”背后的安全保卫战
- 【Golang】快速复习指南QuickReview(七)——interface
- OHOS LTS 3.0移植到RaspberryPi 4B
- 20省市公布元宇宙路线图
- TCP/UDP基本原理
- Why is only one value displayed on your data graph?
- 区块哈希竞猜游戏系统开发(dapp)
- What are the requirements for new bonds? Is it safe to play new bonds
- 【Golang】深究字符串——从byte rune string到Unicode与UTF-8
- Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
猜你喜欢

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City

How to use the low code platform of the Internet of things for process management?

基于 ShardingSphere 的得物数据库中间件平台“彩虹桥”演进之路

Game asset reuse: a new way to find required game assets faster

Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用

Open source SPL redefines OLAP server

Syntax of SQL union query (inline, left, right, and full)

Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL

基于微信小程序的婚纱影楼小程序开发笔记

Elastricearch's fragmentation principle of the second bullet
随机推荐
Elastricearch's fragmentation principle of the second bullet
Implementing MySQL fuzzy search with node and express
Goldfish rhca memoirs: do447 managing user and team access -- effectively managing users with teams
ZABBIX monitoring - Aruba AP operation data
Importance and purpose of test
Add two factor authentication, not afraid of password disclosure, let alone 123456
如何使用物联网低代码平台进行流程管理?
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
Ready to migrate to the cloud? Please accept this list of migration steps
LeetCode 473. 火柴拼正方形
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
Kubernetes resource topology aware scheduling optimization
Is it safe to make new debt
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
Ugeek's theory 𞓜 application and design of observable hyperfusion storage system
区块哈希竞猜游戏系统开发(dapp)
What are the requirements for new bonds? Is it safe to play new bonds
解读2022年度敏捷教练行业现状报告
怎么开户?在国海证券开户安全吗?需要带什么?
Open source SPL redefines OLAP server