golang 安装gin包、创建路由基本总结

发布于:2025-05-19 ⋅ 阅读:(10) ⋅ 点赞:(0)


一、安装gin包和热加载包

  1. 首先终端新建一个main.go
  2. 然后go mod init ‘项目名称’
  3. 执行以下命令 安装gin包
go get -u github.com/gin-gonic/gin
  1. 终端安装热加载包
go get github.com/pilu/fresh
  1. 终端输入fresh 运行 ,之后文件的改动,项目自动重新热加载
fresh
  1. 简单示例代码
package main

import (
	"github.com/gin-gonic/gin"
)
func main (){
	r := gin.Default() // 创建一个路由引擎
	// 配置路由
	r.GET("/", func(c *gin.Context) {
		c.String(200, "你好,gin")
	})
}

二、路由简单场景总结

  1. 返回string数据
    /*1.创建路由*/
	r := gin.Default() // 创建一个路由引擎
	// 配置路由
	r.GET("/", func(c *gin.Context) {
		c.String(200, "你好,gin")
	})
	
	// 返回string数据
	r.POST("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "你好,gin") //http.StatusOK 表示200的状态码
	})
  1. 返回json数据
  r.GET("/json", func(c *gin.Context) {
		//gin.H是 map[string]interface{} 的类型别名:用于快速创建键值对形式JSON
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"data": gin.H{
				"name": "张三",
				"age":  18,
			},
		})
	})
  1. 返回结构体
	
 type Article struct {
	Title   string `json:"title"` //json序列化时的键名,Title在json中以小写展示
	Desc    string `json:"desc"`
	Content string `json:"content"`
  }


   r.GET("/json1", func(c *gin.Context) {
		a := &Article{Title: "标题", Desc: "描述", Content: "内容"} //使用指针是避免每次都要拷贝结构体,不使用指针也可以访问数据
		//gin.H是 map[string]interface{} 的类型别名:用于快速创建键值对形式JSON
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"data": a,
		})

	})
  1. jsonp返回
   type Article struct {
	Title   string `json:"title"` //json序列化时的键名,Title在json中以小写展示
	Desc    string `json:"desc"`
	Content string `json:"content"`
    }
  
  
   	// http://localhost:8080/jsonp?callback=xxx
	//返回 xxx({"code":200,"data":{"title":"标题-jsonp","desc":"描述","content":"内容"}});
	r.GET("/jsonp", func(c *gin.Context) {
		a := &Article{Title: "标题-jsonp", Desc: "描述", Content: "内容"}
		//gin.H是 map[string]interface{} 的类型别名:用于快速创建键值对形式JSON
		c.JSONP(http.StatusOK, gin.H{
			"code": 200,
			"data": a,
		})
	})
  1. 返回XML
  r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{
			"code":  200,
			"title": "小米SU7",
		})

	})
  1. 返回模版 html
   	/*向html中传入普通数据*/
   r.GET("/html", func(c *gin.Context) {
		 c.HTML(http.StatusOK, "goods.html", gin.H{ 
		   "code": 200,
		   "title": `小米SU7`,
		 })
		 
		/*html中使用 .title接收*/
	  <h1 class="text-center my-4">{{.title}}</h1>
   
	/*向html中传入传入结构体*/
	r.GET("/html", func(c *gin.Context) {
		b := &Article{Title: "小米SU7年轻人的第一辆车", Desc: "描述", Content: "内容"} 
		c.HTML(http.StatusOK, "goods.html", gin.H{
			"code":  "200",
			"data":  b,
		})
	})


	/* 结构体赋值变量  $t */
	 {{$t := .data.Title}}
      <h3>{{$t}}</h3>
      
	/* 解构结构体  with */
	 {{with .data}}
      <p>{{.Title}}</p>
      {{end}}
	/*向html中传入变量,模版条件判断*/
	r.GET("/html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "goods.html", gin.H{
			"code":  "200",
			"price": 50,
		})
	})

	 <!-- 条件判断 eq 相等 / ne 不相等 /  gt 大于 / lt 小于 / ge 大于等于 / le 小于等于 -->
      {{if ge .price 80}}
       <p>有点贵:{{.price}}</p>
       {{else if lt .price 10}}
       <p>有点便宜:{{.price}}</p>
       {{else}}
       <p>价格:{{.price}}</p>
       {{end}}
       /*html中传入切片,模版循环遍历数据*/
		r.GET("/html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "goods.html", gin.H{
			"code":  "200",
			"hobby": []string{"小米", "SU7", "YU7"},
		  })
	   })


	 <!-- 循环遍历数据 -->
     {{range $key,$value := .hobby}}
      <li>{{$key}}---{{$value}}</li>
       {{else}}
      <p>没有数据</p>
      {{end}}

网站公告

今日签到

点亮在社区的每一天
去签到