go中gin框架+realize實現邊寫代碼邊編譯,熱更新


最近看到了熱加載,相關的,就搜索了goland實現熱加載

發現了一個插件realize

https://github.com/oxequa/realize

然后,為了自己擼代碼更方便,配合gin寫個教程

 

1.准備

go get github.com/oxequa/realize
go get github.com/gin-gonic/gin
2.然后開始

package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

func main() {
r := gin.Default()
r.GET("/sayHello/:name", SayHello)
r.GET("/test/:id/:name", getUser)
r.Run(":9090")
}

//http://localhost:9090/test/1/dong
func getUser(c *gin.Context) {
id := c.Param("id")
name := c.Param("name")
json := gin.H{
"data": id,
"name": name,
}
c.JSON(http.StatusOK, json)
}

//http://localhost:9090/sayHello/dong
func SayHello(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "hello,"+name)
//c.String(http.StatusOK, "change,"+name)
}
3.項目目錄

 

4.其中.r開頭文件都是生成的,生成步驟如下:

terminal運行:
realize init
一直next就行,可以按需配置

生成.realize.yaml后
terminal運行:
realize start

5.啟動后如圖

 

6.訪問http://localhost:9090/sayHello/dong

 

7.然后更改方法內的內容

 

8.提示重新build並且重啟了 

 

 9.然后重新訪問http://localhost:9090/sayHello/dong

 

10.這樣就是簡單的實現了熱加載代碼改變,大家可以互相討論,我也是第一次用

備注,把.realize.yaml分享一下

settings:
files:
outputs:
status: true
path: ""
name: .r.outputs.log
logs:
status: true
path: ""
name: .r.logs.log
errors:
status: true
path: ""
name: .r.errors.log
flimit: 100
legacy:
force: false
interval: 0s
server:
status: true
open: false
port: 9090
host: localhost
schema:
- name: /Users/dong/go/src/awesomeProject
path: /Users/dong/go/src/awesomeProject/main
commands:
clean:
status: true
vet:
status: true
fmt:
status: true
test:
status: true
generate:
status: true
install:
status: true
build:
status: true
run:
status: true
watcher:
extensions:
- go
paths:
- /
ignore:
paths:
- .git
- .realize
- vendor
————————————————
版權聲明:本文為CSDN博主「學生董格」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/aaaadong/java/article/details/91983589


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM