Install golang air tool in Macbook M1
Installation
Tried to install this “Live Reload Tool” air in MacOS. In the README page, it listed 2 ways to install:
1, Use the go get way: go get -u github.com/cosmtrek/air , but this will only install an old version v1.12, which doesn’t support auto read .air.toml as its default config. If you already created a .air.toml in your project and want to just run air inside that folder w/o explicitly specify the config file path, then you need a newer version
2, It also listed the bash way:
1 | curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin |
But this doesn’t work for Macbook M1 chip, b/c at the day of writing this post, it doesn’t have a OS=darwin, Arch=arm64 in its release page. The script tries to fetch air_1.15.1_darwin_arm64.tar.gz but it isn’t there.
So we need to build it manually:
- Clone the git:
git clone https://github.com/cosmtrek/air.git - Check to a released version:
git checkout v1.15.1 - Run
make build, this will generate binary fileair - Copy it to your go bin path:
cp air $HOME/go/bin(make sure go bin path is in your $PATH env) - Run
air versionto confirm it has been successfully installed:air version(should print v.1.15.1)
Config file
In your project folder, the .air.toml may look like this:
1 | # . or absolute path, please note that the directories following must be under root |
Then under your project folder, you can just run $ air. When you change something like your .go and .html file, it will auto reload. One thing I don’t like though is there is a glitch window, b/c air needs to shutdown the old process and start the new one, if you refresh page in this glitch window, your page will be broken. You will need to wait for 1 second to refresh again after the new process started, although it is a short time window, but it is still annoying when you experienced lots of this case.