zoukankan      html  css  js  c++  java
  • 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{}
    }
    

      

  • 相关阅读:
    C#中yield return用法分析
    SQL表连接查询(inner join(join)、full join、left join、right join、cross join)
    SQL Server 数据类型转换函数
    linq查询结果指定列的两种方式
    html2cavans
    ORM
    Node.js Web 模块
    Node.js GET/POST请求
    Node.js 文件系统
    装逼利器函数注释
  • 原文地址:https://www.cnblogs.com/finnlee/p/14259911.html
Copyright © 2011-2022 走看看