目录
  • 概述
  • 安装
  • 测试
    • 导包
    • 步骤
  • 切换输出的格式
    • 状态码
      • 示例
        • 前端

          概述

          Gin是一个golang的微框架,封装比较优雅,API友好。具有快速灵活,容错方便等特点。Gin自身的net/http足够简单,性能也非常不错

          Gin下载: https://github.com/gin-gonic/gin
          英文文档:https://gin-gonic.com/docs/

          安装

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

          测试

          导包

          import "github.com/gin-gonic/gin"   
          import "net/http"   //项目中使用了 http.StatusOK
          

          步骤

          注册一个路由器

          router := gin.Default()
          

          注册路由处理

          router.GET("/", func(c *gin.Context) {
             c.String(http.StatusOK, "Hello World")
          })
          

          运行(默认是8080端口)

          if true{
            router.Run()  //默认端口:8080     http://localhost
          }else{
            router.Run(":9999")  //指端端口:9999    http://localhost:9999
          }
          

          切换输出的格式

          返回json格式

          func (c *Context) JSON(code int, obj interface{})
          

          返回xml格式

          func (c *Context) XML(code int, obj interface{})
          

          返回yaml格式

          func (c *Context) YAML(code int, obj interface{})
          

          返回string格式

          func (c *Context) String(code int, format string, values ...interface{})
          

          渲染html模板后返回

          func (c *Context) HTML(code int, name string, obj interface{})
          

          状态码

          这个状态码不仅可以手动指定一个数字,比如200,500,404;也可以使用http包中的状态码,语义化的状态码更好理解;

          http-status文档:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
          http状态码详解:http://tool.oschina.net/commons?type=5

          HTTP状态码的分类

          分类 描述
          1** 信息,服务器收到请求,需要请求者继续执行操作
          2** 成功,操作被成功接收并处理
          3** 重定向,需要进一步的操作以完成请求
          4** 客户端错误,请求包含语法错误或无法完成请求
          5** 服务器错误,服务器在处理请求的过程中发生了错误

          常用的状态码

          200 请求成功
          404 请求失败,服务器无法根据客户端的请求找到资源(网页)
          500 服务器内部错误,无法完成请求

          项目中导入

          import "net/http"
          
          package http
          
          const (  
              StatusOK                   = 200 // RFC 7231, 6.3.1
              StatusMultipleChoices   = 300 // RFC 7231, 6.4.1
              StatusNotFound                     = 404 // RFC 7231, 6.5.4
              StatusInternalServerError           = 500 // RFC 7231, 6.6.1
          )
          

          示例

          package main
          import(
            "github.com/gin-gonic/gin"
            "net/http"
            "fmt"
          )
          func main() {
            //1. 注册一个路由器
            router := gin.Default()
            //2. 注册路由处理
            //默认请求  http://localhost:8080/
            router.GET("/", func(c *gin.Context) {
               c.String(http.StatusOK, fmt.Sprintln(gin.H{"data":"默认请求"}))
            })
            //post 请求  string 格式话   http://localhost:8080/string
            router.GET("/string", func(c *gin.Context) {
               c.String(http.StatusOK, fmt.Sprintln("post 请求  string 格式话"))
            })
            //post 请求  json 格式话    http://localhost:8080/json
            router.POST("/json",func (c *gin.Context)  {
              c.JSON(http.StatusOK,gin.H{"name":"post 请求  json 格式话","age":18})
            })
            //delete 请求 xml 格式化   http://localhost:8080/xml
            router.DELETE("/xml",func (c *gin.Context)  {
              c.XML(http.StatusOK,gin.H{"name":"delete 请求 xml 格式化","age":18})
            })
            //patch 请求  yaml 格式化   http://localhost:8080/yaml
            router.PATCH("/yaml",func (c *gin.Context)  {
              c.YAML(http.StatusOK,gin.H{"name":"patch 请求 yaml 格式化","age":18})
            })
            //get请求 html界面显示   http://localhost:8080/html
            router.GET("/html",func (c *gin.Context)  {
              router.LoadHTMLGlob("../view/tem/index/*")  //这是前台的index
              // router.LoadHTMLGlob("../view/tem/admin/*")  //这是后台的index
              // router.LoadHTMLFiles("../view/tem/index.html")  //指定加载某些文件
              c.HTML(http.StatusOK,"index.html",nil)
            })
            //3. 运行(默认是8080端口)
            router.Run()
          }
          

          前端

          路径:$GOPATH/src/view/tem/index/

          <!DOCTYPE html>
          <html lang="zh-cn" dir="ltr">
            <head>
              <meta charset="utf-8">
              <title></title>
            </head>
            <body>
              <h1>前端 index</h1>
            </body>
          </html>
          

          以上就是golang微框架Gin的安装测试及简介的详细内容,更多关于Gin安装测试及简介的资料请关注其它相关文章!

          声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。