gin多数据格式返回结果
1.返回byte和string类型
context.Writer.Write([]byte("fullpath="+fullpath))
context.Writer.WriteString("fullpath"+fullpath)
2.返回JSON
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main(){
engine := gin.Default()
engine.GET("/hello", func(context *gin.Context) {
fullpath := context.FullPath()
context.Writer.Write([]byte("fullpath="+fullpath))
context.Writer.WriteString("fullpath"+fullpath)
})
//map类型的
engine.GET("/hellojson", func(context *gin.Context) {
fullpath := context.FullPath()
context.JSON(200, map[string]interface{}{
"code":200,
"msg":"OK",
"data":fullpath,
})
})
//结构体类型
engine.GET("/hellostruct", func(context *gin.Context) {
fullpath := context.FullPath()
resp := Response{
Code: 200,
Msg: "OK",
Data: fullpath,
}
context.JSON(200,&resp)
})
//模板渲染
engine.LoadHTMLGlob("./gin01/html/*")//设置html访问路径
engine.Static("/img","./img") //第一个参数是前端访问的 第二个参数是本地的
engine.GET("/helloshtml", func(context *gin.Context) {
fullpath := context.FullPath()
context.HTML(http.StatusOK,"index.html",gin.H{
"fullpath":fullpath,
}) //传值到html 页面{{.fullpath}}渲染
})
engine.Run()
}
type Response struct {
Code int
Msg string
Data interface{}
}

浙公网安备 33010602011771号