0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Go 言語を学ぶAdvent Calendar 2023

Day 20

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

Posted at

air

インストール

 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
 ls bin/
air

初期化処理

 ./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が生成されたようです。

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関数の作成

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を起動してみます。

 ./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サーバーが起動しました。

きちんとサーバーは起動できているようです。

$ curl localhost:8081
{"status":200,"message":"Hello, World!"}

ファイルを編集してみる

ホットリロードを試すのに、main.goを編集します。レスポンスのMessageを変更しました。

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

変更を検知し、リロードが走りました。

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
$ curl localhost:8081
{"status":200,"message":"Hello by air"}

よさそうです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?