• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Gin框架学习 | 青训营

武飞扬头像
我要找到祢
帮助1

Gin框架学习

gin快速开始

1.设置代理

go env -w GOPROXY=https://goproxy.cn,direct

2.远程拉取(加上版本号,默认拉去最新的,需要新版go环境支撑)

go get -u github.com/gin-gonic/gin@v1.8

3.导入包

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

4.入门案例

package main   //一个项目只能有一个mian包
improt(
     "github.com/gin-gonic/gin"  
)
func main(){
 
       // 创建一个默认的路由引擎
        route=gin.Default()
      //配置路由
        route.GET("/",func(ctx *gin.Context) {
             //向浏览器返回JSON格式的数据,gin自带JSON序列化器,不用像就Java等要导入
               ctx.JSON(200,gin.H{
                      "message": "入生苦短,我用golang!",
        })
})
         //启动HTTP服务,默认8080端口
           route.Run()
         //若要改变端口,使用 route.Run(":8081"),不要漏了前面:
}
Restful 接口规范

Rest是web服务的一种架构风格,一种设计风格,是一种思想;同时Rest不是针对某一种编程语言的。

非Rest设计:

http://localhost:8080/admin/getUser (查询用户)

http://localhost:8080/admin/addUser (新增用户)

http://localhost:8080/admin/updateUser (更新用户)

http://localhost:8080/admin/deleteUser (删除用户)

Rest风格:

GET http://localhost:8080/admin/user (查询用户)

POST http://localhost:8080/admin/user (新增用户)

PUT http://localhost:8080/admin/user (更新用户)

DELETE http://localhost:8080/admin/user (删除用户)

总结:URL只指定资源,以HTTP方法动词进行不同的操作。用HTTP STATUS/CODE定义操作结果。

状态码

200~299段 表示操作成功:

200 操作成功,正常返回

201 操作成功,已经正在处理该请求

300~399段 表示参数方面的异常

300 参数类型错误

301 参数格式错误

302 参数超出正常取值范围

303 token过期

304 token无效

400~499段 表示请求地址方面的异常:

400 找不到地址

500~599段 表示内部代码异常:

500 服务器代码异常
gin基本操作
    // 创建一个服务
    ginServer := gin.Default()
    ginServer.Use(favicon.New("./Arctime.ico")) // 这里如果添加了东西然后再运行没有变化,请重启浏览器,浏览器有缓存
    // 加载静态页面
    ginServer.LoadHTMLGlob("templates/*") // 一种是全局加载,一种是加载指定的文件

    // 加载资源文件
    ginServer.Static("/static", "./static")

    // 相应一个页面给前端
    ginServer.GET("/index", func(ctx *gin.Context) {
        ctx.HTML(http.StatusOK, "index.html", gin.H{
            "msg": "This data is come from Go background.",
        })
    })
    // 下面是传统方式的例子
    ginServer.GET("/user/info", func(context *gin.Context) { // 这个格式是固定的
        userid := context.Query("userid")
        username := context.Query("username")
        // 拿到之后返回给前端
        context.JSON(http.StatusOK, gin.H{
            "userid":   userid,
            "username": username,
        })
    })
    // 此时执行代码之后,在浏览器中可以输入http://localhost:8081/user/info?userid=111&username=666
    // 就可以看到返回了JSON格式的数据

    // 下面是Rustful方式的例子
    ginServer.GET("/user/info/:userid/:username", func(context *gin.Context) {
        userid := context.Param("userid")
        username := context.Param("username")
        // 还是一样,返回给前端
        context.JSON(http.StatusOK, gin.H{
            "userid":   userid,
            "username": username,
        })
    })
    // 指定代码后,只需要在浏览器中http://localhost:8081/user/info/111/555
    // 就可以看到返回了JSON数据了,非常方便简洁
    
        // 处理表单请求 这些都是支持函数式编程,Go语言特性,可以把函数作为参数传进来
    ginServer.POST("/user/add", func(ctx *gin.Context) {
        username := ctx.PostForm("username")
        password := ctx.PostForm("password")
        ctx.JSON(http.StatusOK, gin.H{
            "msg":      "ok",
            "username": username,
            "password": password,
        })
    })

    // 路由
    ginServer.GET("/test", func(ctx *gin.Context) {
        // 重定向 -> 301
        ctx.Redirect(301, "https://conqueror712.gitee.io/conqueror712.gitee.io/")
    })
    // http://localhost:8081/test


    // 服务器端口,用服务器端口来访问地址
    ginServer.Run(":8081") // 不写的话默认是8080,也可以更改

    // 序列化
    // 前端给后端传递JSON
    ginServer.POST("/json", func(ctx *gin.Context) {
        // request.body
        data, _ := ctx.GetRawData()
        var m map[string]interface{} // Go语言中object一般用空接口来表示,可以接收anything
        // 顺带一提,1.18以上,interface可以直接改成any
        _ = json.Unmarshal(data, &m)
        ctx.JSON(http.StatusOK, m)
    })


这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkgbbc
系列文章
更多 icon
同类精品
更多 icon
继续加载