One minute
Gin使用笔记
创建HTTP服务器响应 HTML 内容
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
热加载 Live Reload
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
# 安装
go get github.com/codegangsta/gin
# 运行
gin run main.go
视图 views
package main
import (
"github.com/gin-gonic/gin"
"github.com/foolin/gin-template"
"net/http"
)
func main() {
router := gin.Default()
//new template engine
router.HTMLRender = gintemplate.Default()
router.GET("/page", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "page.html", gin.H{"title": "Page file title!!"})
})
router.Run(":9090")
}
目录结构
|-- app/views/
|--- index.html
|--- page.html
|-- layouts/
|--- footer.html
|--- master.html