栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Gin:路由配置和获取参数

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gin:路由配置和获取参数

目录
    • 1. 路由配置
    • 2. 获取get请求传值
    • 3. 获取get请求,path传值
    • 4. 获取post请求传值
    • 5. 获取post form请求,结构体传值
    • 6. 获取post json请求,结构体传值

VSCode 搭建Gin
Gin:路由配置和获取参数
Gin:路由抽离与分组
Gin:自定义控制器及控制器继承
Gin:中间件
Gin:文件上传
Gin:Cookie及二级域名共享Cookie
Gin:Session

1. 路由配置
package main

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

type UserInfo struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
	Age      int    `form:"age" json:"age"`
}

type Department struct {
	Id       int    `json:"id"`
	DeptName string `json:"deptName"`
}

func main() {

	// 创建一个默认的路由引擎
	r := gin.Default()
	// 配置静态web目录;第一个参数表示路由,第二个参数表示映射的目录
	r.Static("/static", "./static")
	// 配置路由
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"message": "Hello world!",
		})
	})

	// 获取get请求传值
	r.GET("/login", func(c *gin.Context) {
		username := c.Query("username")
		// 如果没传,则使用默认值123
		password := c.DefaultQuery("password", "123")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name":     username,
			"password": password,
		})
	})

	// 获取get请求,path传值
	r.GET("/login/:username", func(c *gin.Context) {
		username := c.Param("username")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name": username,
		})
	})

	// 获取post请求传值
	r.POST("/login", func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.PostForm("password")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name":     username,
			"password": password,
		})
	})

	// 获取post form请求,结构体传值
	r.POST("/loginForm", func(c *gin.Context) {
		user := &UserInfo{}
		err := c.ShouldBind(&user)
		if err == nil {
			fmt.Printf("%#v n", user)
			c.JSON(http.StatusOK, user)
		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}
	})

	// 获取post json请求,结构体传值
	r.POST("/loginJson", func(c *gin.Context) {
		dept := &Department{}
		err := c.ShouldBindJSON(&dept)
		if err == nil {
			fmt.Printf("%#v n", dept)
			c.JSON(http.StatusOK, dept)
		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}
	})

	// 启动 HTTP 服务,默认在 0.0.0.0:8080 启动服务
	r.Run()

}

2. 获取get请求传值
r.GET("/login", func(c *gin.Context) {
	username := c.Query("username")
	// 如果没传,则使用默认值123
	password := c.DefaultQuery("password", "123")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name":     username,
		"password": password,
	})
})
  • http://localhost:8080/login?username=fisher

3. 获取get请求,path传值
r.GET("/login/:username", func(c *gin.Context) {
	username := c.Param("username")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name": username,
	})
})
  • http://localhost:8080/login/fisher

4. 获取post请求传值
r.POST("/login", func(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name":     username,
		"password": password,
	})
})
  • http://localhost:8080/login

5. 获取post form请求,结构体传值
type UserInfo struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
	Age      int    `form:"age" json:"age"`
}
r.POST("/loginForm", func(c *gin.Context) {
	user := &UserInfo{}
	err := c.ShouldBind(&user)
	if err == nil {
		fmt.Printf("%#v n", user)
		c.JSON(http.StatusOK, user)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": err.Error(),
		})
	}
})
  • http://localhost:8080/loginForm

6. 获取post json请求,结构体传值
type Department struct {
	Id       int    `json:"id"`
	DeptName string `json:"deptName"`
}
r.POST("/loginJson", func(c *gin.Context) {
	dept := &Department{}
	err := c.ShouldBindJSON(&dept)
	if err == nil {
		fmt.Printf("%#v n", dept)
		c.JSON(http.StatusOK, dept)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": err.Error(),
		})
	}
})
  • http://localhost:8080/loginJson

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991458.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号