Go語(yǔ)言中Http響應(yīng)的實(shí)現(xiàn)
在Gin框架中可以使用gin.Context對(duì)象的String() JSON() XML() HTML() FILE()等方法生成Http響應(yīng).
1.字符串響應(yīng)請(qǐng)求:
func response() {
request := gin.Default()
request.GET("/stringResponse", func(context *gin.Context) {
context.String(http.StatusOK, "hello string response")
})
}
通過(guò)string方法發(fā)送了200狀態(tài)碼和字符串給客戶端.
2.動(dòng)態(tài)字符串響應(yīng):
func response() {
response := "動(dòng)態(tài)響應(yīng)"
request := gin.Default()
request.GET("/stringResponse", func(context *gin.Context) {
context.String(http.StatusOK, "hello %s", response)
})
}
響應(yīng)字符串包含了response的值.實(shí)現(xiàn)了動(dòng)態(tài)內(nèi)容輸出.
3.HTML方式響應(yīng):
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_html", func(c *gin.Context) {
htmlContent := "<h1>Hi,this an html response</h1>"
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(htmlContent))
})
request.Run()
}
執(zhí)行結(jié)果:

4.json格式響應(yīng)請(qǐng)求:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_json", func(c *gin.Context) {
jsonData := gin.H{
"message": "hello world",
"status": 200,
}
c.JSON(http.StatusOK, jsonData)
})
request.Run()
}
執(zhí)行結(jié)果:

5.Go結(jié)構(gòu)體Json響應(yīng):
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_json", func(c *gin.Context) {
jsonData := JsonData{
Message: "Hello World!",
Status: 200,
}
c.JSON(http.StatusOK, jsonData)
})
request.Run()
}
type JsonData struct {
Message string `json:"message"`
Status int `json:"status"`
}
執(zhí)行結(jié)果:

6.XML格式響應(yīng):
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_xml", func(c *gin.Context) {
xmlData := gin.H{
"message": "hello xml",
"status": 200,
}
c.XML(http.StatusOK, xmlData)
})
request.Run()
}
執(zhí)行結(jié)果:

7.結(jié)構(gòu)體定義XML響應(yīng):
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_xml", func(c *gin.Context) {
xmlData := JsonData{
Message: "Hello World!",
Status: 200,
}
c.XML(http.StatusOK, xmlData)
})
request.Run()
}
8.設(shè)置Http響應(yīng)頭:
gin.Context對(duì)象的Header方法用于設(shè)置單個(gè)響應(yīng)頭.該方法接收兩個(gè)參數(shù),響應(yīng)頭名稱和對(duì)應(yīng)的值:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_header", func(c *gin.Context) {
c.Header("X-Custom-Header", "this is a custom header")
c.String(http.StatusOK, "this is a custom header")
})
request.Run()
}
9.設(shè)置多個(gè)響應(yīng)頭:
可以通過(guò)多次調(diào)用Header方法來(lái)設(shè)置多個(gè)響應(yīng)頭.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_header", func(c *gin.Context) {
c.Header("X-Custom-Header-One", "this is a custom header")
c.Header("X-Custom-Header-Two", "this is a custom header")
c.Header("X-Custom-Header-Three", "this is a custom header")
c.String(http.StatusOK, "this is a custom header")
})
request.Run()
}
10.重定向設(shè)置響應(yīng)頭:
在執(zhí)行重定向之前,使用gin.Context對(duì)象的Header方法設(shè)置響應(yīng)頭,然后使用Redirect進(jìn)行重定向.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
request := gin.Default()
request.GET("/hi_header", func(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8")
c.Redirect(http.StatusTemporaryRedirect, "http://www.google.com")
})
request.Run()
}
到此這篇關(guān)于Go語(yǔ)言中Http響應(yīng)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Go語(yǔ)言Http響應(yīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go語(yǔ)言編程之美自定義二進(jìn)制文件實(shí)用指南
這篇文章主要介紹了go語(yǔ)言編程之美自定義二進(jìn)制文件實(shí)用指南2023-12-12
golang中的io.ReadCloser與ioutil.NopCloser使用
這篇文章主要介紹了golang中的io.ReadCloser與ioutil.NopCloser使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Golang實(shí)現(xiàn)http文件上傳小功能的案例
這篇文章主要介紹了Golang實(shí)現(xiàn)http文件上傳小功能的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05
Go?interface{}?轉(zhuǎn)切片類型的實(shí)現(xiàn)方法
本文主要介紹了Go?interface{}?轉(zhuǎn)切片類型的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Go語(yǔ)言基礎(chǔ)學(xué)習(xí)之map的示例詳解
哈希表是常見(jiàn)的數(shù)據(jù)結(jié)構(gòu),有的語(yǔ)言會(huì)將哈希稱作字典或者映射,在Go中,哈希就是常見(jiàn)的數(shù)據(jù)類型map,本文就來(lái)聊聊Golang中map的相關(guān)知識(shí)吧2023-04-04

