<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 響應 其實在[流程](../chaptero1/0101_flow.md) 中就有講到。 在請求來到服務器后,Context對象會生成用來串流程: 和請求有關的字段包括: ``` // context.go:40 type Context struct { // 用來響應 Writer ResponseWriter writermem responseWriter } // response_writer.go:20 type ResponseWriter interface { http.ResponseWriter //嵌入接口 http.Hijacker //嵌入接口 http.Flusher //嵌入接口 http.CloseNotifier //嵌入接口 // 返回當前請求的 response status code Status() int // 返回寫入 http body的字節數 Size() int // 寫string WriteString(string) (int, error) //是否寫出 Written() bool // 強制寫htp header (狀態碼 + headers) WriteHeaderNow() } // response_writer.go:40 // 實現 ResponseWriter 接口 type responseWriter struct { http.ResponseWriter size int status int } ``` ### 初始化過程 在請求來到服務器時,會從對象池中拿到一個Context對象; ``` // 1 初始化writermem // gin.go:322 c.writermem.reset(w) func (w *responseWriter) reset(writer http.ResponseWriter) { w.ResponseWriter = writer w.size = noWritten w.status = defaultStatus } // 2 初始化context // gin.go:324 c.reset() func (c *Context) reset() { c.Writer = &c.writermem c.Params = c.Params[0:0] c.handlers = nil c.index = -1 c.Keys = nil c.Errors = c.Errors[0:0] c.Accepted = nil } ``` ### 設置響應碼、cookie、header等 ``` // 實現原理:設置c.writermen.status Status(code int) // 設置response code // 實現原理: 調用系統函數 Header(key, value string) // 設置header // 實現原理: 調用系統函數 SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) ``` ### 設置返回的數據 ``` Render(code int, r render.Render) // 數據渲染 HTML(code int, name string, obj interface{}) //HTML JSON(code int, obj interface{}) //JSON IndentedJSON(code int, obj interface{}) SecureJSON(code int, obj interface{}) JSONP(code int, obj interface{}) //jsonp XML(code int, obj interface{}) //XML YAML(code int, obj interface{}) //YAML String(code int, format string, values ...interface{}) //string Redirect(code int, location string) // 重定向 Data(code int, contentType string, data []byte) // []byte File(filepath string) // file SSEvent(name string, message interface{}) // Server-Sent Event Stream(step func(w io.Writer) bool) // stream ``` 我們仔細看看實現邏輯: ``` // 實現有一個 Render接口 // render/render.go:9 type Render interface { Render(http.ResponseWriter) error WriteContentType(w http.ResponseWriter) } // 自己選擇具體的實現,有 - JSON - IndentedJSON - SecureJSON - JsonpJSON - XML - String - Redirect - Data - HTML - HTMLDebug - HTMLProduction - YAML - MsgPack // 對應實現進行具體操作,完成數據輸出 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看