GoFrame 數(shù)據(jù)返回-基本介紹

2022-04-14 09:26 更新

?HTTP Server?的數(shù)據(jù)返回通過(guò)?ghttp.Response?對(duì)象實(shí)現(xiàn),?ghttp.Response?對(duì)象實(shí)現(xiàn)了標(biāo)準(zhǔn)庫(kù)的?http.ResponseWriter?接口。數(shù)據(jù)輸出使用?Write*?相關(guān)方法實(shí)現(xiàn),并且數(shù)據(jù)輸出采用了?Buffer?機(jī)制,因此數(shù)據(jù)的處理效率比較高。任何時(shí)候可以通過(guò)?OutputBuffer?方法輸出緩沖區(qū)數(shù)據(jù)到客戶端,并清空緩沖區(qū)數(shù)據(jù)。

常用方法:更詳細(xì)的接口列表請(qǐng)參考 https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#Response

func (r *Response) Write(content ...interface{})
func (r *Response) WriteExit(content ...interface{})
func (r *Response) WriteJson(content interface{}) error
func (r *Response) WriteJsonExit(content interface{}) error
func (r *Response) WriteJsonP(content interface{}) error
func (r *Response) WriteJsonPExit(content interface{}) error
func (r *Response) WriteOver(content ...interface{})
func (r *Response) WriteOverExit(content ...interface{})
func (r *Response) WriteStatus(status int, content ...interface{})
func (r *Response) WriteStatusExit(status int, content ...interface{})
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error
func (r *Response) WriteTplContent(content string, params ...gview.Params) error
func (r *Response) WriteTplDefault(params ...gview.Params) error
func (r *Response) WriteXml(content interface{}, rootTag ...string) error
func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error
func (r *Response) Writef(format string, params ...interface{})
func (r *Response) WritefExit(format string, params ...interface{})
func (r *Response) Writefln(format string, params ...interface{})
func (r *Response) WriteflnExit(format string, params ...interface{})
func (r *Response) Writeln(content ...interface{})
func (r *Response) WritelnExit(content ...interface{})

簡(jiǎn)要說(shuō)明:

  • ?Write*?方法用于往返回的數(shù)據(jù)緩沖區(qū)追加寫(xiě)入數(shù)據(jù),參數(shù)可為任意的數(shù)據(jù)格式,內(nèi)部通過(guò)斷言對(duì)參數(shù)做自動(dòng)分析。
  • ?Write*Exit?方法用于往返回的數(shù)據(jù)緩沖區(qū)追加寫(xiě)入數(shù)據(jù)后退出當(dāng)前執(zhí)行的?HTTP Handle?r方法,可用于替代?return?返回方法。
  • ?WriteOver*?方法用于覆蓋緩沖區(qū)寫(xiě)入,原有緩沖區(qū)的數(shù)據(jù)將會(huì)被覆蓋為新寫(xiě)入的數(shù)據(jù)。
  • ?WriteStatus*?方法用于設(shè)置當(dāng)前請(qǐng)求執(zhí)行返回的狀態(tài)碼。
  • ?WriteJson*/WriteXml?方法用于特定數(shù)據(jù)格式的輸出,這是為開(kāi)發(fā)者提供的簡(jiǎn)便方法。
  • ?WriteTpl*?方法用于模板輸出,解析并輸出模板文件,也可以直接解析并輸出給定的模板內(nèi)容。
  • 其他方法詳見(jiàn)接口文檔;

此外,需要提一下,?Header?的操作可以通過(guò)標(biāo)準(zhǔn)庫(kù)的方法來(lái)實(shí)現(xiàn),例如:

Response.Header().Set("Content-Type", "text/plain; charset=utf-8")


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)