Hot reload with Go + Air

Introduction

As a tool to hot reload when changing files in Go I would like to use cosmtrek/air.

environment

go: v1.15.3

Project creation

% mkdir sample && cd sample
% go mod init
% go mod init sample

Anything is fine here, but I use gin as a framework.

% go get -u github.com/gin-gonic/gin
% vi main.go
package main

import "github.com/gin-gonic/gin"

func main()  {
  r := gin.Default()
  r.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "response",
    })
  })

  r.Run()
}

go run I will start it with go run.

% go run main.go

When you get up, in another window,

% curl http://localhost:8080
{"message":"response"}

Move with air

Introduction

go get -u github.com/cosmtrek/air
% which air
<GOPATH>/go/bin/air

% air -v
  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ v1.12.1 // live reload for Go apps, with Go1.14.0

When air comes in, run it with the air command instead of go run.

$ air

Rewrite main.go when it starts.

package main

import "github.com/gin-gonic/gin"

func main()  {
  r := gin.Default()
  r.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "It's a response",
    })
  })

  r.Run()
}
% curl http://localhost:8080
{"message":"It's a response"}

I was able to hot reload.

Recommended Posts

Hot reload with Go + Air
Python with Go
Draw Bezier curves with Go
Bit full search with Go
Connect to Postgresql with GO
Try implementing perfume with Go
reload in django shell with ipython
Learn algorithms with Go @ recursive call
GUI development with Fyne by Go
Go see whales with combinatorial optimization
Upload & move GCS files with Go
Using Lambda with AWS Amplify with Go
Using cgo with the go command
Metaprogram go with YAML + Mustache + go-generate
Use curl / jq library with Go
Operate Linux Network Namespace with Go