golang開發(fā)微框架Gin的安裝測試及簡介
概述
Gin是一個golang的微框架,封裝比較優(yōu)雅,API友好。具有快速靈活,容錯方便等特點(diǎn)。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.StatusOK
步驟
注冊一個路由器
router := gin.Default()
注冊路由處理
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)碼
這個狀態(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** | 重定向,需要進(jìn)一步的操作以完成請求 |
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. 注冊路由處理 //默認(rèn)請求 http://localhost:8080/ router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, fmt.Sprintln(gin.H{"data":"默認(rèn)請求"})) }) //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. 運(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的安裝測試及簡介的詳細(xì)內(nèi)容,更多關(guān)于Gin安裝測試及簡介的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文帶大家了解Go語言中的內(nèi)聯(lián)優(yōu)化
內(nèi)聯(lián)優(yōu)化是一種常見的編譯器優(yōu)化策略,通俗來講,就是把函數(shù)在它被調(diào)用的地方展開,這樣可以減少函數(shù)調(diào)用所帶來的開銷,本文主要為大家介紹了Go中內(nèi)聯(lián)優(yōu)化的具體使用,需要的可以參考下2023-05-05Go中time.RFC3339 時間格式化的實(shí)現(xiàn)
這篇文章主要介紹了Go中time.RFC3339 時間格式化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01解決golang 反射interface{}做零值判斷的一個重大坑
這篇文章主要介紹了解決golang 反射interface{}做零值判斷的一個重大坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04