Golang の Air とは
Air は、Go 言語で開発されたホットリロードツールです。コードを変更するたびに自動的にビルドと再起動を行い、開発効率を大幅に向上させることができます。
主な機能
- コード変更を監視し、自動的にビルドと再起動を行う
- テスト実行を自動化
- コードカバレッジの測定
- デバッガの統合
メリット
- 開発効率の向上
- コード変更の迅速な検証
- テストの自動化による品質向上
- デバッグの容易化
使い方
Air を使用するには、以下の手順が必要です。
1. Air をインストールする
go get -u github.com/cosmtrek/air@latest or go install github.com/cosmtrek/air@latest
macだとpathの追加が必要になるかと思います
export PATH=$PATH:$(go env GOPATH)/bin
2. .air.toml ファイルを構成する
プロジェクトフォルダのルートにきて .air.tomlを作成する
air.toml
ファイルは、Airの設定をカスタマイズするための構成ファイルです。このファイルを編集することで、Airの動作を詳細に制御できます。以下は、air.toml
ファイルの主要な設定項目についての説明と例です。
基本的なair.toml
ファイルの例
# Config file for Air - Live reload for Go applications [build] # Working directory # (default: .) cwd = "." # Name of binary output file # (default: "app") bin = "app" # Delays the execution of the binary (in milliseconds) # (default: 0) delay = 1000 # Build arguments to pass to `go build` # (default: []) args = ["-race", "-v"] # Directory to store temporary files # (default: os.TempDir()) tmp_dir = "tmp" # Patterns to exclude from watching exclude_dir = ["assets", "tmp"] # Patterns to include for watching include_dir = ["."] # Patterns to exclude specific file types exclude_file = ["*.tmp", "*.log"] [log] # Log level: debug, info, warning, error, fatal # (default: "info") level = "debug" # Log format: text, json # (default: "text") format = "text" [events] # Commands to run before the build starts before_build = ["echo 'Before build'"] # Commands to run after the build finishes after_build = ["echo 'After build'"] [http] # HTTP proxy for live reload # (default: "") proxy = "" # Address for the server # (default: "localhost:9090") address = "localhost:9090"
各設定項目の詳細
[build]
- cwd: 作業ディレクトリ。デフォルトは現在のディレクトリです。
- bin: 出力バイナリの名前。デフォルトは
app
です。 - delay: バイナリの実行を遅延させる時間(ミリ秒)。デフォルトは0です。
- args:
go build
に渡す引数。デフォルトは空の配列です。 - tmp_dir: 一時ファイルを格納するディレクトリ。デフォルトはOSの一時ディレクトリです。
- exclude_dir: 監視から除外するディレクトリのパターン。
- include_dir: 監視対象に含めるディレクトリのパターン。
- exclude_file: 監視から除外するファイルのパターン。
delayについて
build.delay
の設定は、Goのアプリケーションの再ビルドと再実行の間に遅延を挿入するために使用されます。
この遅延は、特定の条件下で有用です。以下にその利用例と理由をいくつか挙げます。
遅延の使用例と理由
リソースの解放時間を確保:
- アプリケーションが停止した後、特定のリソース(例えばネットワーク接続やファイルハンドルなど)の解放には時間がかかる場合があります。すぐに再起動すると、これらのリソースがまだ解放されていないためにエラーが発生することがあります。遅延を設定することで、リソースが適切に解放されるのを待つことができます。
外部サービスの安定化:
- アプリケーションが外部サービス(データベース、APIなど)と連携している場合、頻繁な再起動は接続の確立や安定性に問題を引き起こすことがあります。遅延を挿入することで、外部サービスとの再接続がスムーズに行われるようにします。
開発環境の負荷軽減:
- 大規模なプロジェクトやリソース集約型のビルドプロセスでは、頻繁な再ビルドがシステムリソースに負担をかけることがあります。遅延を設定することで、頻繁な再ビルドを避け、システムの負荷を軽減できます。
ログの確認時間の確保:
- ビルドが失敗した場合や、エラーメッセージを確認したい場合に、遅延を設定することでログを確認する時間を確保できます。
air.toml
ファイルの[build]
セクションにdelay
を設定します。以下は、その例です:
[build] # ビルドの遅延を1000ミリ秒(1秒)に設定 delay = 1000
[log]
- level: ログレベル。デフォルトは
info
です。debug
、info
、warning
、error
、fatal
のいずれかを指定できます。 - format: ログフォーマット。
text
またはjson
を指定できます。デフォルトはtext
です。
[events]
- before_build: ビルド開始前に実行するコマンドのリスト。
- after_build: ビルド完了後に実行するコマンドのリスト。
[http]
- proxy: ライブリロード用のHTTPプロキシ。デフォルトは空文字列です。
- address: サーバーのアドレス。デフォルトは
localhost:9090
です。
インストールに失敗したお話
なぜかインストールできない事があったので記載します。 状況としては air というコマンドを発行しても認識しないです
D:\git\golang\study01>go install github.com/cosmtrek/air@latest go: github.com/cosmtrek/air@latest: version constraints conflict: github.com/cosmtrek/air@v1.52.2: parsing go.mod: module declares its path as: github.com/air-verse/air but was required as: github.com/cosmtrek/air D:\git\golang\study01>air 'air' は、内部コマンドまたは外部コマンド、 操作可能なプログラムまたはバッチ ファイルとして認識されていません。
PowerShellを管理者として実行するか、コマンドプロンプトを開きます。
Airをcloneする
cd c:temp; git clone https://github.com/cosmtrek/air.git
- cloneしたプログラムからインストールする
cd air go install
- プロジェクトフォルダに移動してAirを起動する
air __ _ ___ / /\ | | | |_) /_/--\ |_| |_| \_ (devel), built with Go go1.22.3 mkdir D:\git\golang\study01\tmp watching . watching fibertest watching helloworld watching test1 !exclude tmp building... running... start fiber ┌───────────────────────────────────────────────────┐ │ Fiber v2.52.4 │ │ http://127.0.0.1:3000 │ │ (bound on host 0.0.0.0 and port 3000) │ │ │ │ Handlers ............. 6 Processes ........... 1 │ │ Prefork ....... Disabled PID ............. 25840 │ └───────────────────────────────────────────────────┘