<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 內容渲染 Web處理程序可以返回各種內容類型,例如,JSON,純文本,圖像等。 通常在與API通信時,可以指定并接收內容類型,以闡明將傳入數據的格式以及要接收的數據。 本節將通過第三方庫對數據格式進行切換。 ### 實踐 1. 獲取第三方庫: ``` go get github.com/unrolled/render ``` 2. 建立 negotiate.go: ``` package negotiate import ( "net/http" "github.com/unrolled/render" ) // Negotiator 封裝render并對ContentType進行一些切換 type Negotiator struct { ContentType string *render.Render } // GetNegotiator 接收http請求 并從Content-Type標頭中找出ContentType func GetNegotiator(r *http.Request) *Negotiator { contentType := r.Header.Get("Content-Type") return &Negotiator{ ContentType: contentType, Render: render.New(), } } ``` 3. 建立 respond.go: ``` package negotiate import "io" import "github.com/unrolled/render" // Respond 根據 Content Type 判斷應該返回什么樣類型的數據 func (n *Negotiator) Respond(w io.Writer, status int, v interface{}) { switch n.ContentType { case render.ContentJSON: n.Render.JSON(w, status, v) case render.ContentXML: n.Render.XML(w, status, v) default: n.Render.JSON(w, status, v) } } ``` 4. 建立 handler.go: ``` package negotiate import ( "encoding/xml" "net/http" ) // Payload 甚至數據模板 type Payload struct { XMLName xml.Name `xml:"payload" json:"-"` Status string `xml:"status" json:"status"` } // Handler 調用GetNegotiator處理返回格式 func Handler(w http.ResponseWriter, r *http.Request) { n := GetNegotiator(r) n.Respond(w, http.StatusOK, &Payload{Status: "Successful!"}) } ``` 5. 建立 main.go: ``` package main import ( "fmt" "net/http" "github.com/agtorre/go-cookbook/chapter7/negotiate" ) func main() { http.HandleFunc("/", negotiate.Handler) fmt.Println("Listening on port :3333") err := http.ListenAndServe(":3333", nil) panic(err) } ``` 6. 運行: ``` $ go run main.go Listening on port :3333 $curl "http://localhost:3333 -H "Content-Type: text/xml" <payload><status>Successful!</status></payload> $curl "http://localhost:3333 -H "Content-Type: application/json" {"status":"Successful!"} ``` ### 說明 github.com/unrolled/render 包可以幫助你處理各種類型的請求頭。請求頭通常包含多個值,您的代碼必須考慮到這一點。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <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>

                              哎呀哎呀视频在线观看