[Awesome Go] Use air to hot live reload for Go apps

air

When I get started with developing websites in Go and Iris or Gin framework, it’s a pity that gin lacks live-reloading.

Air is yet another live-reloading command line utility for Go applications in development. Just air in your project root directory, leave it alone, and focus on your code.

Features

  • Colorful log output

  • Customize build or binary command

  • Support excluding subdirectories

  • Allow watching new directories after Air started

  • Better building process

Installation

The classic way to install

Remember to repalce col-air-example with your prefer folder.

1
2
3
$ cd col-air-example

$ go get -u github.com/cosmtrek/air

Configuration

  1. create a new file
1
$ touch .air.toml
  1. paste air_example.toml into this file, and modify it to satisfy your needs.

See the complete air_example.toml - https://github.com/cosmtrek/air/blob/master/air_example.toml

Run

Run air with your config. If file name is .air.toml, just run air.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/go/bin/air    
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ v1.12.1 // live reload for Go apps, with Go1.14.0

watching .
!exclude tmp
building...
running...
Now listening on: http://localhost:8080
Application started. Press CMD+C to shut down.
# You have modify main.go file.
main.go has changed
building...
running...
Now listening on: http://localhost:8080
Application started. Press CMD+C to shut down.

Debug

air -d prints all logs.

Docker

Please pull this docker image cosmtrek/air.

1
2
3
4
5
6
7
docker run -it --rm \
-w "<PROJECT>" \
-e "air_wd=<PROJECT>" \
-v $(pwd):<PROJECT> \
-p <PORT>:<APP SERVER PORT> \
cosmtrek/air
-c <CONF>

For example, one of my project runs in docker:

1
2
3
4
5
docker run -it --rm \
-w "/go/src/github.com/cosmtrek/hub" \
-v $(pwd):/go/src/github.com/cosmtrek/hub \
-p 9090:9090 \
cosmtrek/air

References

[1] cosmtrek/air: ☁️ Live reload for Go apps - https://github.com/cosmtrek/air

[2] air/air_example.toml at master · cosmtrek/air - https://github.com/cosmtrek/air/blob/master/air_example.toml

[3] cosmtrek/air - Docker Hub - https://hub.docker.com/r/cosmtrek/air

[4] air · pkg.go.dev - https://pkg.go.dev/github.com/cosmtrek/air

[5] The Go Programming Language - https://golang.org/