As a tool to hot reload when changing files in Go I would like to use cosmtrek/air.
go: v1.15.3
% 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"}
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