go中gin框架+realize实现边写代码边编译,热更新,方便边改边看效果 - Go语言中文社区

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/article/details/91983589
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-26 22:58:47
  • 阅读 ( 3712 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢