1. Live Reload vs Hot Reload
When developing with Visual Studio Code as an IDE, hot reloading can be achieved with cosmtrek/air. air itself is a development aid implemented in Golang and its main features include
- listening for changes to files in the application folder and restarting the application
- customisation via configuration files
- the ability to set files to be ignored and other helper options
The installation of the Air is fairly simple.
After installation, execute the air init
command in the project root directory to initialise the air configuration and generate the default configuration file.
|
|
By starting the application with the command air -c .air.toml
, air will listen to the content described in the configuration file above and reload the entire application when a file is modified or deleted, updated, etc., thus enabling Live Reload.
2. JetBrains Goland solution in practice
If you are using JetBrains Goland as your IDE, you don’t need to use air to do Live Reload; after all, air is compiled and run directly using go build -o, and debug debugging is not supported; Goland is implemented using the Save Actions plugin to listen for save actions, and when the When the “Save Actions” are triggered, the application is launched with Goland’s configured Run or Debug options.
2.1. Configuring the Debug or Run option for the application
Locate the application entry main function and enable the Debug or Run option. Configure the necessary parameters to start the application according to the pop-up box.
2.2. Download and install the Save Actions plug-in
In the Plugin Centre, search for and install the ‘Save Actions’ plugin. You can see that the plugin’s description does not actually include support for Goland. In practice, however, the plugin works well in a Goland environment.
2.3. Configuring the Save Actions Plugin
Configure the Save Actions plug-in in the Preferences section, mainly by selecting the Debug or Run option that you have edited in the first step in the “Build Actions” section, as follows:
After the above configuration, editing a file in Goland and saving it will automatically execute the Debug or Run option. The plugin’s interface also allows you to specify a regular expression for “files not to be monitored”, so that unnecessary files can be excluded from listening.