2021-07-05-golang-gin框架

本文详细介绍了Gin框架,包括其简介、安装步骤、使用示例,如RESTful API、JSON、XML格式处理、querystring、form表单、path参数绑定、文件上传、重定向、路由分组、中间件应用及自定义HTTP配置、日志和cookie的设置。通过实例展示了Gin在Go语言中的高效和便捷。
摘要由CSDN通过智能技术生成


简介

(官方)Gin is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.

官方地址:https://github.com/gin-gonic/gin

中文文档:https://www.kancloud.cn/shuangdeyu/gin_book/949433

注意:使用gin需要Go的版本号为1.6或更高

安装

安装gin所需的依赖包

go get -u github.com/gin-gonic/gin

在代码中导入

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

使用示例

快速入门

package main

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

func sayHello(c *gin.Context) {
   
	c.JSON(200, gin.H{
   
		"message": "Hello Golang!",
	})
}

func main() {
   
	// 创建一个默认的路由引擎
	r := gin.Default()
	// 指定 用户使用GET请求访问 /hello时,执行sayHello这个函数
	r.GET("/hello", sayHello)
	// 启动服务,默认8080端口,可以指定为9090
	r.Run(":9090")
}

不同的请求方式-RESTful API

HTTP协议中四种不同的请求方法

  • GET用来获取资源
  • POST用来新建资源
  • PUT用来更新资源
  • DELETE用来删除资源
    在这里插入图片描述
func main() {
   
	r := gin.Default()
	r.GET("/book", func(c *gin.Context) {
   
		c.JSON(http.StatusOK, gin.H{
   
			"message": "GET",
		})
	})

	r.POST("/book", func(c *gin.Context) {
   
		c.JSON(200, gin.H{
   
			"message": "POST",
		})
	})

	r.PUT("/book", func(c *gin.Context) {
   
		c.JSON(200, gin.H{
   
			"message": "PUT",
		})
	})

	r.DELETE("/book", func(c *gin.Context) {
   
		c.JSON(200, gin.H{
   
			"message": "DELETE",
		})
	})
}

返回JSON、XML等格式

XML、JSON、YAML和ProtoBuf 渲染

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

	// gin.H 是一个map类型 map[string]interface{}
	r.GET("/someJSON", func(c *gin.Context) {
   
		c.JSON(http.StatusOK, gin.H{
   "message": "hey", "status": http.StatusOK})
	})

	r.GET("/moreJSON", func(c *gin.Context) {
   
		// 也可以使用结构体,但是结构体中的字段首字母需要大写,小写则不可导出
    // 如下在结构体中加一个tag,可以使得输出的内容为 user
		var msg struct {
   
			Name    string `json:"user"`
			Message string
			Number  int
		}
		msg.Name = "Lena"
		msg.Message = "hey"
		msg.Number = 123
		// Note that msg.Name becomes "user" in the JSON
		// Will output  :   {"user": "Lena", "Message": "hey", "Number": 123}
		c.JSON(http.StatusOK, msg)
	})

	r.GET("/someXML", func(c *gin.Context) {
   
		c.XML(http.StatusOK, gin.H{
   "message": "hey", "status": http.StatusOK})
	})

	r.GET("/someYAML", func(c *gin.Context) {
   
		c.YAML(http.StatusOK, gin.H{
   "message": "hey", "status": http.StatusOK})
	})

	r.GET("/someProtoBuf", func(c *gin.Context) {
   
		reps := []int64{
   int64(1), int64(2)}
		label := "test"
		// The specific definition of protobuf is written in the testdata/protoexample file.
		data := &protoexample.Test{
   
			Label: &label,
			Reps:  reps,
		}
		// Note that data becomes binary data in the response
		// Will output protoexample.Test protobuf serialized data
		c.ProtoBuf(http.StatusOK, data)
	})

	// Listen and serve on 0.0.0.0:8080
	r.Run(":8080")
}

获取querystring请求参数

querystring是什么

query string是传递给这个程序的URL的一部分, 通过对它的使用, 可以允许我们从HTTP的client端发送数据给生成web page的应用程序.

GET请求,URL中?后面的即为query string参数,为key-value格式

例如:

https://cn.bing.com/search?q=querystring

示例

package main

import (
	"net/http"

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

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

	// 获取浏览器发请求携带的 query string 参数
	r.GET("/web", func(c *gin.Context) {
   
		// 三种通过query获取请求中携带的 query string 参数
		// name := c.Query("query")
		// name := c.DefaultQuery("query","somebody")
		name, ok := c.GetQuery("query")
		if !ok {
   
			// 取不到值情况
			name = "somebody"
		}
		c.JSON(http.StatusOK, gin.H{
   
			"Name": name,
		})
	})
	r.Run(":9090")
}

//请求 http://192.168.166.89:9090/web?query=1233445
//返回 {"Name":"1233445"}

获取form表单参数

login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值