【Go】airを用いたホットリロード開発

# air



## インストール
```bash
 curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s
cosmtrek/air info checking GitHub for latest tag
cosmtrek/air info found version: 1.49.0 for v1.49.0/darwin/arm64
cosmtrek/air info installed ./bin/air
```

```bash
 ls bin/
air
```

初期化処理
```bash
 ./bin/air init

  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ 1.49.0, built with Go 1.21.3

.air.toml file created to the current directory with the default settings
```
`.air.toml`が生成されたようです。
```toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  post_cmd = []
  pre_cmd = []
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

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

[log]
  main_only = false
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true

```


## main関数の作成

```golang
package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

type ResponseData struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

type RequestData struct {
	Message string `json:"message"`
}

func main() {
	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.GET("/", hello)

	e.POST("error", func(c echo.Context) error {
		panic("error")
	})
	e.Logger.Fatal(e.Start(":8081"))
}

func hello(c echo.Context) error {
	data := ResponseData{
		Status:  200,
		Message: "Hello, World!",
	}
	return c.JSON(http.StatusOK, data)
}

```

airを起動してみます。
```bash
 ./bin/air

  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ 1.49.0, built with Go 1.21.3

watching .
watching bin
!exclude tmp
building...
running...

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.11.3
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:8081
```

ビルドが走り、echoサーバーが起動しました。

きちんとサーバーは起動できているようです。
```bash
$ curl localhost:8081
{"status":200,"message":"Hello, World!"}
```

## ファイルを編集してみる
ホットリロードを試すのに、main.goを編集します。レスポンスのMessageを変更しました。
```golang:main.go
func hello(c echo.Context) error {
	data := ResponseData{
		Status:  200,
		Message: "Hello by air",
	}
	return c.JSON(http.StatusOK, data)
}

```

変更を検知し、リロードが走りました。
```bash
main.go has changed
building...
running...

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.11.3
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:8081
```

```bash
$ curl localhost:8081
{"status":200,"message":"Hello by air"}
```

よさそうです。