golang開發(fā)微框架Gin的安裝測(cè)試及簡(jiǎn)介
概述
Gin是一個(gè)golang的微框架,封裝比較優(yōu)雅,API友好。具有快速靈活,容錯(cuò)方便等特點(diǎn)。Gin自身的net/http足夠簡(jiǎn)單,性能也非常不錯(cuò)
Gin下載: https://github.com/gin-gonic/gin
英文文檔:https://gin-gonic.com/docs/
安裝
go get -u github.com/gin-gonic/gin
測(cè)試
導(dǎo)包
import "github.com/gin-gonic/gin" import "net/http"http://項(xiàng)目中使用了 http.StatusOK
步驟
注冊(cè)一個(gè)路由器
router := gin.Default()
注冊(cè)路由處理
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World")
})
運(yùn)行(默認(rèn)是8080端口)
if true{
router.Run() //默認(rèn)端口: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{})
狀態(tài)碼
這個(gè)狀態(tài)碼不僅可以手動(dòng)指定一個(gè)數(shù)字,比如200,500,404;也可以使用http包中的狀態(tài)碼,語(yǔ)義化的狀態(tài)碼更好理解;
http-status文檔:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
http狀態(tài)碼詳解:http://tool.oschina.net/commons?type=5
HTTP狀態(tài)碼的分類
| 分類 | 描述 |
|---|---|
| 1** | 信息,服務(wù)器收到請(qǐng)求,需要請(qǐng)求者繼續(xù)執(zhí)行操作 |
| 2** | 成功,操作被成功接收并處理 |
| 3** | 重定向,需要進(jìn)一步的操作以完成請(qǐng)求 |
| 4** | 客戶端錯(cuò)誤,請(qǐng)求包含語(yǔ)法錯(cuò)誤或無(wú)法完成請(qǐng)求 |
| 5** | 服務(wù)器錯(cuò)誤,服務(wù)器在處理請(qǐng)求的過(guò)程中發(fā)生了錯(cuò)誤 |
常用的狀態(tài)碼
200 請(qǐng)求成功
404 請(qǐng)求失敗,服務(wù)器無(wú)法根據(jù)客戶端的請(qǐng)求找到資源(網(wǎng)頁(yè))
500 服務(wù)器內(nèi)部錯(cuò)誤,無(wú)法完成請(qǐng)求
項(xiàng)目中導(dǎo)入
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. 注冊(cè)一個(gè)路由器
router := gin.Default()
//2. 注冊(cè)路由處理
//默認(rèn)請(qǐng)求 http://localhost:8080/
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, fmt.Sprintln(gin.H{"data":"默認(rèn)請(qǐng)求"}))
})
//post 請(qǐng)求 string 格式話http://localhost:8080/string
router.GET("/string", func(c *gin.Context) {
c.String(http.StatusOK, fmt.Sprintln("post 請(qǐng)求 string 格式話"))
})
//post 請(qǐng)求 json 格式話 http://localhost:8080/json
router.POST("/json",func (c *gin.Context) {
c.JSON(http.StatusOK,gin.H{"name":"post 請(qǐng)求 json 格式話","age":18})
})
//delete 請(qǐng)求 xml 格式化http://localhost:8080/xml
router.DELETE("/xml",func (c *gin.Context) {
c.XML(http.StatusOK,gin.H{"name":"delete 請(qǐng)求 xml 格式化","age":18})
})
//patch 請(qǐng)求 yaml 格式化http://localhost:8080/yaml
router.PATCH("/yaml",func (c *gin.Context) {
c.YAML(http.StatusOK,gin.H{"name":"patch 請(qǐng)求 yaml 格式化","age":18})
})
//get請(qǐng)求 html界面顯示http://localhost:8080/html
router.GET("/html",func (c *gin.Context) {
router.LoadHTMLGlob("../view/tem/index/*") //這是前臺(tái)的index
// router.LoadHTMLGlob("../view/tem/admin/*") //這是后臺(tái)的index
// router.LoadHTMLFiles("../view/tem/index.html") //指定加載某些文件
c.HTML(http.StatusOK,"index.html",nil)
})
//3. 運(yùn)行(默認(rèn)是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的安裝測(cè)試及簡(jiǎn)介的詳細(xì)內(nèi)容,更多關(guān)于Gin安裝測(cè)試及簡(jiǎn)介的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信