当前位置: 首页 > news >正文

gin框架

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) { //配置路由
		c.String(200, "%v", "你好") //状态码和格式化输出的内容
	})
	r.POST("/add", func(c *gin.Context) {
		c.String(200, "123")
	})
	r.PUT("/put", func(c *gin.Context) {
		c.String(http.StatusOK, "123")
	})
	r.Run(":8000")
}

同时可以使用热加载的工具辅助

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Article struct {
	Title   string
	Desc    string
	Content string
}

func main() {
	r := gin.Default()
	r.LoadHTMLGlob("templates/*")
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{ //json数据
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{ //别名
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/json", func(c *gin.Context) {
		a := &Article{
			Title:   "123",
			Content: "1234",
			Desc:    "545",
		}
		c.JSON(http.StatusOK, a)
	})
	r.GET("/jsonp", func(c *gin.Context) { //处理跨域
		a := &Article{
			Title:   "123",
			Content: "1234",
			Desc:    "545",
		}
		c.JSONP(http.StatusOK, a)
	})
	r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{ //json数据
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/news", func(c *gin.Context) {
		c.HTML(http.StatusOK, "news.html", gin.H{ //json数据
			"success": true,
			"msg":     "hasd",
		})
	})
	r.Run()
}

json,jsonp,xml,HTML等数据的加载,注意需要渲染模板r.LoadHTMLGlob("templates/*")

r.LoadHTMLGlob("templates/**/*")

**表示一层目录,*表示所有文件,这时候需要给html文件起个别名

{{define "xxxxx"}}

{{end}}

输出变量通过{{.xxx}}

定义变量输出变量

{{$t:=.success}}
        {{$t}}

if条件判断

range遍历

with解构数据,直接通过.访问

自定义模板函数

package main

import (
	"net/http"
	"text/template"
	"time"

	"github.com/gin-gonic/gin"
)

type Article struct {
	Title   string
	Desc    string
	Content string
}

func UnixToTime(timestamp int) string {
	t := time.Unix(int64(timestamp), 0)
	return t.Format("2006-01-02 15:04:05")
}
func main() {
	r := gin.Default()
	r.SetFuncMap(template.FuncMap{
		"UnixToTime": UnixToTime,
	})
	r.LoadHTMLGlob("templates/*")
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{ //json数据
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{ //别名
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/json", func(c *gin.Context) {
		a := &Article{
			Title:   "123",
			Content: "1234",
			Desc:    "545",
		}
		c.JSON(http.StatusOK, a)
	})
	r.GET("/jsonp", func(c *gin.Context) { //处理跨域
		a := &Article{
			Title:   "123",
			Content: "1234",
			Desc:    "545",
		}
		c.JSONP(http.StatusOK, a)
	})
	r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{ //json数据
			"success": true,
			"msg":     "hasd",
		})
	})
	r.GET("/news", func(c *gin.Context) {
		c.HTML(http.StatusOK, "news.html", gin.H{ //json数据
			"success": true,
			"msg":     "hasd",
			"date":    1741746247,
		})
	})
	r.Run()
}
<body>
    <h2>
       
        {{UnixToTime .date}}
    </h2>
</body>

注意要写在加载模板之前

引入外部公共模板

引入静态文件

get方法传参获取

	r.GET("/get", func(c *gin.Context) {
		username := c.Query("username")
		age := c.Query("age")
		page := c.DefaultQuery("page", "1")
		c.JSON(http.StatusOK, map[string]interface{}{ //json数据
			"success": username,
			"age":     age,
			"page":    page,
		})
	})
r.POST("/post", func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.PostForm("password")
		c.JSON(http.StatusOK, map[string]interface{}{ //json数据
			"username": username,
			"password": password,
		})
	})

获取post传参

	r.POST("/post", func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.DefaultPostForm("password", "213")
		c.JSON(http.StatusOK, map[string]interface{}{ //json数据
			"username": username,
			"password": password,
		})
	})

获取get post传递的数据绑定到结构体

type UserInfo struct {
	Username string `json:"username" form:"username"`
	Password string `json:"password" form:"password"` //json表示把返回的进行小写,后边匹配的表单
}

r.GET("/getUser", func(c *gin.Context) {
		user := &UserInfo{}
		if err := c.ShouldBind(&user); err == nil {
			c.JSON(http.StatusOK, user)
		} else {
			c.JSON(http.StatusOK, gin.H{
				"err": err.Error(),
			})
		}
	})

xml

type AArticle struct {
	Title   string `json:"title" xml:"title"`
	Content string `json:"password" xml:"content"`
}

	r.POST("/postXml", func(c *gin.Context) {
		xmlSliceData, _ := c.GetRawData()
		article := &AArticle{}
		if err := xml.Unmarshal(xmlSliceData, &article); err == nil {
			c.JSON(http.StatusOK, article)
		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}
	})

动态路由

	r.GET("/list/:cid", func(c *gin.Context) {
		cid := c.Param("cid")
		c.String(200, "%v", cid)
	})

路由分组

apiRouters := r.Group("/api")
	{
		apiRouters.GET("/", func(c *gin.Context) {
			c.String(200, "asdasd")
		})
		apiRouters.GET("/sad", func(c *gin.Context) {
			c.String(200, "sad")
		})
	}

相关文章:

  • ORACLE EBS数据库RELINK方式搭建克隆环境
  • 黑色RGB是什么
  • C#实现AES-CBC加密工具类(含完整源码及使用教程)
  • 浮点数 NaN 彻底研究(linux 下 c环境测试)
  • 贝壳找房:以 OceanBase 为 JuiceFS 元数据引擎,构建 AI 存储底座
  • tomcat配置应用----server.xml文件具体配置
  • Redis Redis介绍、安装 - Redis客户端
  • Linux-基础开发工具
  • 【Academy】JWT 分析 ------ JWT
  • element-plus中form表单组件的使用
  • Python爬虫---中国大学MOOC爬取数据(文中有数据集)
  • 软件工程概述
  • pg_实例架构图解
  • 使用jest测试用例之入门篇
  • python高效试用17---两个字符串组成一个新的字符串和两个字符串组成元组作为key哪个更高效
  • STM32 HAL库 CAN过滤器配置
  • 【网络编程】原始套接字编程
  • 【UI自动化框架第五张】AndroidUiAutomation 类功能简介
  • deepseek R1提供的3d迷宫设计方案
  • freeswitch(多台服务器级联)
  • 仓库管理软件/南通seo
  • 做电影网站被告版权/百度top排行榜
  • 广西住建局官方网站/郑州学校网站建设
  • 加强服务保障满足群众急需i/sem对seo的影响有哪些
  • 牡丹区建设局网站/免费发布信息网平台
  • 百度网站建设技术/电商网站规划