新闻动态

golang开发微框架Gin的安装测试及简介

发布日期:2022-02-01 08:35 | 文章来源:站长之家

概述

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安装测试及简介的资料请关注本站其它相关文章!

国外服务器租用

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部