当前位置:网站首页>[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 :

  • web In development , Modify for the front-end request request Parameter structure field tag value ;
  • 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 -------------------

原网站

版权声明
本文为[DDGarfield]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231941491013.html