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

  1. listening for changes to files in the application folder and restarting the application
  2. customisation via configuration files
  3. the ability to set files to be ignored and other helper options

The installation of the Air is fairly simple.

1
2
# binary will be $(go env GOPATH)/bin/air
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
1
2
# or install it into ./bin/
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s

After installation, execute the air init command in the project root directory to initialise the air configuration and generate the default configuration file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  bin = "./tmp/myapp"
  cmd = "go build -o ./tmp/myapp ."
  delay = 1000
  exclude_dir = ["tmp", "vendor", "testdata","docker"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html","css","js"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  time = false

[misc]
  clean_on_exit = true

[screen]
  clear_on_rebuild = false

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.

JetBrains Goland JetBrains Goland

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.

Save Actions plug-in

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:

Save Actions plug-in

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.