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" //項(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)文章!
相關(guān)文章
Go操作mongodb數(shù)據(jù)庫(kù)方法示例
這篇文章主要為大家介紹了Go操作mongodb數(shù)據(jù)庫(kù)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09一文帶大家了解Go語(yǔ)言中的內(nèi)聯(lián)優(yōu)化
內(nèi)聯(lián)優(yōu)化是一種常見的編譯器優(yōu)化策略,通俗來(lái)講,就是把函數(shù)在它被調(diào)用的地方展開,這樣可以減少函數(shù)調(diào)用所帶來(lái)的開銷,本文主要為大家介紹了Go中內(nèi)聯(lián)優(yōu)化的具體使用,需要的可以參考下2023-05-05Go中time.RFC3339 時(shí)間格式化的實(shí)現(xiàn)
這篇文章主要介紹了Go中time.RFC3339 時(shí)間格式化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Golang?鎖原理的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了Golang?鎖原理的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03解決golang 反射interface{}做零值判斷的一個(gè)重大坑
這篇文章主要介紹了解決golang 反射interface{}做零值判斷的一個(gè)重大坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04