golang開發(fā)微框架Gin的安裝測試及簡介
概述
Gin是一個golang的微框架,封裝比較優(yōu)雅,API友好。具有快速靈活,容錯方便等特點。Gin自身的net/http足夠簡單,性能也非常不錯
Gin下載: https://github.com/gin-gonic/gin
英文文檔:https://gin-gonic.com/docs/
安裝
go get -u github.com/gin-gonic/gin
測試
導(dǎo)包
import "github.com/gin-gonic/gin" import "net/http"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{})
狀態(tài)碼
這個狀態(tài)碼不僅可以手動指定一個數(shù)字,比如200,500,404;也可以使用http包中的狀態(tài)碼,語義化的狀態(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ù)器收到請求,需要請求者繼續(xù)執(zhí)行操作 |
2** | 成功,操作被成功接收并處理 |
3** | 重定向,需要進一步的操作以完成請求 |
4** | 客戶端錯誤,請求包含語法錯誤或無法完成請求 |
5** | 服務(wù)器錯誤,服務(wù)器在處理請求的過程中發(fā)生了錯誤 |
常用的狀態(tài)碼
200 請求成功
404 請求失敗,服務(wù)器無法根據(jù)客戶端的請求找到資源(網(wǎng)頁)
500 服務(wù)器內(nèi)部錯誤,無法完成請求
項目中導(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. 注冊一個路由器 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的安裝測試及簡介的詳細內(nèi)容,更多關(guān)于Gin安裝測試及簡介的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。