【Gin】Gin 框架的路由


作者的 Gin 框架学习是根据 B站视频 Gin教程_Golang框架Gin入门实战教程 来学习的,对大地老师的评价不吹不捧,很喜欢其讲课风格,而且这个视频特别适合小白学习,强烈推荐。

Gin 框架的路由

路由概述

路由(Routing)是由一个 URL(或者叫路径)和一个特定的 HTTP 方法(GET、POST等)组成的,涉及到应用如何响应客户端对某个网站节点的访问。

RESTful API 是目前比较成熟的一套互联网应用程序的 API 设计理论,所以我们设计我们的路由的时候建议参考 RESTful API 指南。

在 RESTful 架构中,每个网址代表一种资源,不同的请求方式表示执行不同的操作

方法说明
GET(SELECT)从服务器取出资源(一项或多项)
POST(CREATE)在服务器新建一个资源
PUT(UPDATE)在服务器更新资源(客户端提供改变后的完整资源)
DELETE(DELETE)从服务器删除资源

简单的路由配置

简单的路由配置(可以通过 postman测试)

当用 GET 请求访问一个网址的时候,做什么事情:

r.GET("网址",func(c *gin.Context){
	c.String(200,"Get")
})

当用 POST 请求访问一个网址的时候,做什么事情:

r.POST("网址",func(c *gin.Context){
	c.String(200,"Delete")
})

当用 PUT 请求访问一个网址的时候,做什么事情:

r.PUT("网址",func(c *gin.Context){
	c.String(200,"Delete")
})

当用 DELETE 请求访问一个网址的时候,做什么事情:

r.DELETE("网址",func(c *gin.Context){
	c.String(200,"Delete")
})

路由里面获取 GET 传值

域名/news?uid=11

r.GET("/news",func(c *gin.Context){
	aid := c.Query("aid")
	c.String(200,"aid=%s",aid)
})

域名/user/20

r.GET("/user/:uid",func(c *gin.Context){
    uid := c.Param("uid")
	c.String(200,"uid=%s",uid)
})

c.String() c.JSON() c.JSONP() c.XML() c.HTML()

c.String()

package main

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

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(200, "值: %v", "首页")
	})
	r.Run()
}

c.JSON()

package main

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

type Article struct {
	Title   string `json:"title"`
	Desc    string `json:"desc"`
	Content string `json:"content"`
}

func main() {
	r := gin.Default()

	r.GET("/json1", func(c *gin.Context) {
		c.JSON(200, map[string]interface{}{
			"success": true,
			"msg":     "hello gin",
		})
	})
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"msg": "Gin...",
		})
	})
	r.GET("/json3", func(c *gin.Context) {
		article := &Article{
			Title:   "我是一个标题",
			Desc:    "描述",
			Content: "内容",
		}
		c.JSON(200, article)
	})
	r.Run()
}

c.JSONP()

http://localhost:8080/jsonp?callback=xxx

xxx({"title":"我是一个标题-jsonp","desc":"描述","content":"内容"});

package main

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

type Article struct {
	Title   string `json:"title"`
	Desc    string `json:"desc"`
	Content string `json:"content"`
}

func main() {
	r := gin.Default()

	r.GET("/jsonp", func(c *gin.Context) {
		article := Article{
			Title:   "我是一个标题-jsonp",
			Desc:    "描述",
			Content: "内容",
		}
		c.JSONP(200, article)
	})
	r.Run()
}

c.XML()

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{
			"success": true,
			"msg":     "xml...",
		})
	})
	r.Run()
}
<map>
    <success>true</success>
    <msg>xml...</msg>
</map>

c.HTML()

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Article struct {
	Title   string `json:"title"`
	Desc    string `json:"desc"`
	Content string `json:"content"`
}

func main() {
	r := gin.Default()
    // 加载静态页面
	r.LoadHTMLGlob("templates/*")
	
	r.GET("/html", func(c *gin.Context) {
		c.HTML(200, "index.html", gin.H{
			"msg": "hello,html",
		})
	})
	r.Run()
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>GoWeb</title>
    <script src="../static/js/common.js"></script>
</head>
<body>

<h2>{{.msg}}</h2>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值