Live Reload in Go with Air

TheDeveloperCafe
TheDeveloperCafe
Published in
2 min readAug 11, 2022

--

πŸ‘‰ Read the full article here (TheDeveloperCafe Blog) πŸ‘ˆ

Getting Started

In this article, you will learn how to live reload go applications with one of the popular packages out there, air. First, you will learn how to setup up air and start using it right away, then we will see how to customize air with .air.toml file.

Installing air

There are many ways to install air as seen in the installation section of air’s documentation. The way I am going to use it here is via go install (for go install to work you would have to have the $GOPATH/bin in-your-shell path).

go install github.com/cosmtrek/air@latest

Live reload go code

In a new directory create a main.go file with the following content:

package mainimport "fmt"func main() {
fmt.Println("Air")
}
main.go

and run the following command to see live reloading in action:

air

You should see the following output:

watching .
!exclude tmp
building...
running...
Air

Now go ahead change the code and save the file:

package mainimport "fmt"func main() {
fmt.Println("Air Changed") // πŸ‘ˆ update message
}

Air will detect that the file is changed and you will see the new message:

watching .
!exclude tmp
building...
running...
Air Changed

Air creates a directory called tmp where it stores the built binary and executes it.

Configuring air with config file

You can customize Air using a .air.toml file.

Run the following command in the project directory to generate a .air.toml file.

air init

This will generate a .air.toml file with the following output:

…full article is available on my blog TheDeveloperCafe πŸ˜€.

πŸ‘‰ Read the full article here (TheDeveloperCafe Blog) πŸ‘ˆ

--

--