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.项目目录

go中gin框架+realize实现边写代码边编译,热更新,方便边改边看效果_第1张图片

4.其中.r开头文件都是生成的,生成步骤如下:

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

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

5.启动后如图

go中gin框架+realize实现边写代码边编译,热更新,方便边改边看效果_第2张图片

6.访问http://localhost:9090/sayHello/dong

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

go中gin框架+realize实现边写代码边编译,热更新,方便边改边看效果_第3张图片

8.提示重新build并且重启了 

go中gin框架+realize实现边写代码边编译,热更新,方便边改边看效果_第4张图片

 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

 

你可能感兴趣的:(go)