How to use Air to realize Real-time Hot overload of Go Program
Most people do not understand the knowledge points of this article "Air how to achieve the real-time hot overload use of Go programs", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Air how to achieve real-time hot reload use of Go programs" article.
Why do I need to load in real time?
When writing Web projects with Python, the common Flask or Django frameworks support real-time loading. After you modify the project code, the program can be automatically reloaded and executed (live-reload), which is very convenient in the daily development phase.
When using the gin framework of GE language to do local development and debugging, it is often necessary to press Ctrl+C to stop the program and recompile and execute it frequently after changing the code, which is not very convenient.
Air introduction
How can real-time loading function be realized when developing based on gin framework? Troubles like this are certainly not just your troubles, so I started a web-wide search with the mentality that there must be ready-made wheels. Sure enough, I found a tool on Github: Air. It supports the following features:
Color log output
Custom build or binary command
Support for omitting subdirectories
Support listening to new directories after startup
A better build process
Install AirGo
This is also the most classic installation method:
Go get-u github.com/cosmtrek/airMacOScurl-fLo air https://git.io/darwin_airLinuxcurl-fLo air https://git.io/linux_airWindowscurl-fLo air.exe https://git.io/windows_airDcokerdocker run-it-rm\-w ""\-e "air_wd="\-v $(pwd):\-p:\ cosmtrek/air-c
Then run your project in docker as follows:
Docker run-it-- rm\-w "/ go/src/github.com/cosmtrek/hub"\-v $(pwd): / go/src/github.com/cosmtrek/hub\-p 9090VR 9090\ cosmtrek/air use Air
To make it easier and more convenient to type commands, you should add alias air='~/.air' to your .bashrc or .zshrc.
First go to your project directory:
Cd / path/to/your_project
The simplest use is to execute the following command directly:
# first look for the `.air.conf` configuration file under the current directory, and use the default air-c .air.conf if you can't find it
The recommended usage is:
# 1. Create a new configuration file in the current directory. Air.conftouch .air.conf # 2. Copy the contents of `air.conf.example` to this file, and then modify it # 3 according to your needs. Run air with your configuration. If the file name is `.air.conf`, you only need to execute `air`. Airair_example.conf example
The complete air_example.conf sample configuration is as follows, which can be modified according to your own needs.
# [Air] (configuration file in https://github.com/cosmtrek/air) TOML format # working directory # use. Or absolute path, please note that the `tmp_ dir` directory must be under the `root` directory root = "." tmp_dir = "tmp" [build] # only need to write the shell command you normally use for compilation. You can also use `make` # Windows platform example: cmd = "go build-o tmp\ main.exe." cmd = "go build-o. / tmp/main."# binary file name obtained from the `cmd` command # Windows platform example: bin =" tmp\ main.exe "bin =" tmp/main "# Custom commands for executing programs Additional compilation identities can be added such as adding GIN_MODE=release# Windows platform example: full_bin = "tmp\ main.exe" full_bin = "APP_ENV=dev APP_USER=air. / tmp/main" # listens for files with the following file extensions. Include_ext = ["go", "tpl", "tmpl", "html"] # ignore these file extensions or directories exclude_dir = ["assets", "tmp", "vendor" "frontend/node_modules"] # listen for files in the following specified directory include_dir = [] # exclude the following files exclude_file = [] # if the files change too frequently There is no need to trigger the build each time a change is made. You can set the delay time to trigger the build delay = 1000 # ms# to stop running the old binaries when a build error occurs. Stop_on_error = true# air's log file name, which is placed in your `log dir` log = "air_errors.log" [log] # display log time time = true [color] # customize the color displayed in each section. If the color cannot be found, use the original application log. Main = "magenta" watcher = "cyan" build = "yellow" runner = "green" [misc] # Delete tmp directory on exit clean_on_exit = true effect demonstration
The above is about the content of this article on "how to realize the real-time hot reloading of Go programs by Air". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.