Go --- gin基础知识点(一)

简介: Go --- gin基础知识点

使用gin的理由

  • Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确,具有快速灵活,容错方便等特点
  • 对于golang而言,web框架的依赖要远比Python,Java之类的要小。自身的net/http足够简单,性能也非常不错
  • 借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范

简单使用

安装

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

导入

import “github.com/gin-gonic/gin”

hello word示例

package main
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
func main(){
    // 1.创建路由
    r := gin.Default()
    // 2.绑定路由规则,执行的函数
    // gin.Context,封装了request和response
    r.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "hello World!")
    })
    // 3.监听端口,默认在8080
    // Run("里面不指定端口号默认为8080")
    // 指定端口避免:8080这个常用端口被占用
    r.Run(":8000")
}

结果:

gin路由

基本路由

  • gin 框架中采用的路由库是基于httprouter做的
  • 因为虽然net/http这个包里有着默认路由,但是仍存在着不足,所以使用httprouter
  • httprouter 是一个高性能、可扩展的HTTP路由,上面我们列举的net/http默认路由的不足,都被httprouter 实现
  • 要想了解更多的有关httprouter的知识,请访问:Git仓库地址

API

  • 使用Restful风格的API(URL定位资源,用HTTP描述操作)
    1.获取文章 /blog/getXxx Get blog/Xxx
    2.添加 /blog/addXxx POST blog/Xxx
    3.修改 /blog/updateXxx PUT blog/Xxx
    4.删除 /blog/delXxxx DELETE blog/Xxx

参数获取

API参数

  • 通过Context的Param方法来获取API参数
    示例:
package main
import (
    "github.com/gin-gonic/gin"
    "net/http"
    "strings"
)
func main() {
    r := gin.Default()
    r.GET("/user/:name/*action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        //c.String(http.StatusOK,"name = "+name+"action = "+action)
        //截取/
        action = strings.Trim(action, "/")
        c.String(http.StatusOK, name+" is "+action)
    })
    //默认为监听8080端口
    r.Run(":8000")
}

结果:

url参数

  • URL参数可以通过DefaultQuery()或Query()方法获取
  • DefaultQuery()若参数不村则,返回默认值,Query()若不存在,返回空串

示例:

package main
import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
)
func main() {
    r := gin.Default()
    r.GET("/user", func(c *gin.Context) {
        //不指定默认值,若是不传参则接受的是空串
        //name := c.Query("name")
        //指定默认值
        //http://localhost:8080/user 才会打印出来默认的值
        name := c.DefaultQuery("name", "Re")
        c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
    })
    r.Run(":8000")
}

不传参

传参

表单参数

  • 表单传输为post请求,http常见的传输格式为四种:
  1. application/json
  2. application/x-www-form-urlencoded
  3. application/xml
  4. multipart/form-data
  • 表单参数可以通过PostForm()方法获取,该方法默认解析的是x-www-form-urlencoded或from-data格式的参数

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>提交页面</title>
</head>
<body>
    <form action="http://localhost:8000/form" method="post" action="application/x-www-form-urlencoded">
        用户名:<input type="text" name="username" placeholder="请输入你的用户名"> <br>
        密&nbsp码:<input type="password" name="password" placeholder="请输入你的用户名">
        <input type="submit" value="提交">
    </form>
</body>
</html>
package main
import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
)
func main() {
    r := gin.Default()
    r.POST("/form", func(c *gin.Context) {
        // 获取/form的请求类型,及各参数
        types := c.DefaultPostForm("type", "post")
        username := c.PostForm("username")
        password := c.PostForm("password")
        c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
    })
    r.Run(":8000")
}

结果:

上传文件

  • multipart/form-data格式用于文件上传
  • gin文件上传与原生的net/http方法类似,不同在于gin把原生的request封装到c.Request中

单个文件

  • 使用 Request.FormFile 方法获取文件

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传单个文件</title>
</head>
<body>
    <form action="http://localhost:8000/upload" method="post" enctype="multipart/form-data">
      上传文件:<input type="file" name="file" >
      <input type="submit" value="提交">
    </form>
</body>
</html>
package main
import (
    "github.com/gin-gonic/gin"
    "log"
    "net/http"
)
// 上传文件
func main() {
    r := gin.Default()
    //限制上传最大尺寸
    r.MaxMultipartMemory = 8 << 20
    r.POST("/upload", func(c *gin.Context) {
        _, headers, err := c.Request.FormFile("file")
        if err != nil {
            log.Printf("Error when try to get file: %v", err)
        }
        //上传特定的文件
        //headers.Size 获取文件大小
        //if headers.Size > 1024*1024*2 {
        //    fmt.Println("文件太大了")
        //    return
        //}
        //headers.Header.Get("Content-Type")获取上传文件的类型
        //if headers.Header.Get("Content-Type") != "image/png" {
        //    fmt.Println("只允许上传png图片")
        //    return
        //}
        c.SaveUploadedFile(headers, "./video/"+headers.Filename)
        c.String(http.StatusOK, "上传成功" + headers.Filename)
    })
    r.Run(":8000")

结果:

多个文件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>上传多个文件</title>
</head>
<body>
<form action="http://localhost:8000/upload" method="post" enctype="multipart/form-data">
  上传文件:<input type="file" name="files" multiple>
  <input type="submit" value="提交">
</form>
</body>
</html>
func main() {
    // 1.创建路由
    // 默认使用了2个中间件Logger(), Recovery()
    r := gin.Default()
    // 限制表单上传大小 8MB,默认为32MB
    r.MaxMultipartMemory = 8 << 20
    r.POST("/upload", func(c *gin.Context) {
        form, err := c.MultipartForm()
        if err != nil {
            c.String(http.StatusBadRequest, fmt.Sprintf("get err %s", err.Error()))
        }
        // 获取所有图片
        files := form.File["files"]
        // 遍历所有图片
        for _, file := range files {
            // 逐个存
            if err := c.SaveUploadedFile(file, file.Filename); err != nil {
                c.String(http.StatusBadRequest, fmt.Sprintf("upload err %s", err.Error()))
                return
            }
        }
        c.String(200, fmt.Sprintf("upload ok %d files", len(files)))
    })
    //默认端口号是8080
    r.Run(":8000")
}

结果:


相关文章
|
2月前
|
中间件 Go 数据库
Go开发者必读:Gin框架的实战技巧与最佳实践
在当今快速发展的互联网时代,Web开发的需求日益增长。Go语言以其简洁、高效、并发性强的特点,成为了开发者们的首选。而在Go语言的众多Web框架中,Gin无疑是其中的佼佼者。本文将深入探讨Gin框架的特性、优势以及如何利用Gin构建高性能的Web应用。
|
19天前
|
Go 数据安全/隐私保护
go 基于gin编写encode、decode、base64加密接口
go 基于gin编写encode、decode、base64加密接口
15 2
|
2月前
|
SQL 安全 前端开发
Go语言Gin框架安全加固:全面解析SQL注入、XSS与CSRF的解决方案
Go语言Gin框架安全加固:全面解析SQL注入、XSS与CSRF的解决方案
|
2月前
|
Java Go 调度
Go语言并发编程原理与实践:面试经验与必备知识点解析
【4月更文挑战第12天】本文分享了Go语言并发编程在面试中的重要性,包括必备知识点和面试经验。核心知识点涵盖Goroutines、Channels、Select、Mutex、Sync包、Context和错误处理。面试策略强调结构化回答、代码示例及实战经历。同时,解析了Goroutine与线程的区别、Channel实现生产者消费者模式、避免死锁的方法以及Context包的作用和应用场景。通过理论与实践的结合,助你成功应对Go并发编程面试。
38 3
|
9月前
|
开发框架 Go 微服务
Golang 语言怎么使用 go-micro 和 gin 开发微服务?
Golang 语言怎么使用 go-micro 和 gin 开发微服务?
192 0
|
2月前
|
关系型数据库 MySQL Go
go语言使用Gin框架链接数据库
go语言使用Gin框架链接数据库
57 0
|
2月前
|
JavaScript 前端开发 NoSQL
go embed 实现gin + vue静态资源嵌入
go embed 实现gin + vue静态资源嵌入
196 0
|
2月前
|
中间件 Go
go 打印gin 中的c.Request的参数
在 Gin 框架中,可以通过 `c.Request` 获取请求对象,从而访问请求的参数。以下是一个示例,展示如何打印出 `c.Request` 中的参数: ```go package main import ( "fmt" "github.com/gin-gonic/gin" ) func LoggerMiddleware() gin.HandlerFunc { return func(c *gin.Context) { // 打印请求方法和路径 fmt.Printf("开始处理请求: %s %s\n", c.Request.Method, c.Request.URL.Pa
141 0
|
9月前
|
JSON 中间件 Go
Go语言学习 - RPC篇:gin框架的基础能力剖析
gin是非常流行的一款HTTP框架。相较于原生的HTTP server,gin有很多改进点,主要在于3点: 1. 上手简单,开发思路与原生HTTP基本一致 2. 引入多个工具库,提高了开发效率 3. 生态丰富,有许多开源的组件 围绕着gin框架,我们将展开今天的话题。
125 2
Go语言学习 - RPC篇:gin框架的基础能力剖析
|
8月前
|
前端开发 Java 数据库连接
基于Gin+Gorm框架搭建MVC模式的Go语言企业级后端系统
基于Gin+Gorm框架搭建MVC模式的Go语言企业级后端系统
123 0