<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 使用閉包進行狀態處理 將狀態傳遞給處理程序通常很棘手。有兩種方法:通過閉包傳遞狀態,這有助于提高單個處理程序的靈活性,或使用結構體進行傳遞。 我們將使用結構控制器來存儲接口,并使用由外部函數修改的單個處理程序創建兩個路由。 ### 實踐 1. 建立 controller.go: ``` package controllers // Controller 傳遞狀態給處理函數 type Controller struct { storage Storage } func New(storage Storage) *Controller { return &Controller{ storage: storage, } } type Payload struct { Value string `json:"value"` } ``` 2. 建立 storage.go: ``` package controllers // Storage 接口支持存取單個值 type Storage interface { Get() string Put(string) } // MemStorage 實現了 Storage接口 type MemStorage struct { value string } func (m *MemStorage) Get() string { return m.value } func (m *MemStorage) Put(s string) { m.value = s } ``` 3. 建立 post.go: ``` package controllers import ( "encoding/json" "net/http" ) // SetValue 修改Controller的存儲內容 func (c *Controller) SetValue(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(http.StatusMethodNotAllowed) return } if err := r.ParseForm(); err != nil { w.WriteHeader(http.StatusInternalServerError) return } value := r.FormValue("value") c.storage.Put(value) w.WriteHeader(http.StatusOK) p := Payload{Value: value} if payload, err := json.Marshal(p); err == nil { w.Write(payload) } } ``` 4. 建立 get.go: ``` package controllers import ( "encoding/json" "net/http" ) // GetValue是一個封裝HandlerFunc的閉包,如果UseDefault為true,則值始終為“default”,否則它將是存儲在storage中的任何內容 func (c *Controller) GetValue(UseDefault bool) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r.Method != "GET" { w.WriteHeader(http.StatusMethodNotAllowed) return } value := "default" if !UseDefault { value = c.storage.Get() } p := Payload{Value: value} w.WriteHeader(http.StatusOK) if payload, err := json.Marshal(p); err == nil { w.Write(payload) } } } ``` 5. 建立 main.go: ``` package main import ( "fmt" "net/http" "github.com/agtorre/go-cookbook/chapter7/controllers" ) func main() { storage := controllers.MemStorage{} c := controllers.New(&storage) http.HandleFunc("/get", c.GetValue(false)) http.HandleFunc("/get/default", c.GetValue(true)) http.HandleFunc("/set", c.SetValue) 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/set -X POST -d "value=value" {"value":"value"} $curl "http://localhost:3333/get -X GET {"value":"value"} $curl "http://localhost:3333/get/default -X GET {"value":"default"} ``` ### 說明 這種策略有效,因為Go允許函數傳遞。我們可以用類似的方法傳入數據庫連接、日志記錄等。在示例中,我們插入了一個storage接口,所有請求的處理方法都可以使用其方法和屬性。 GetValue方法沒有傳遞http.HandlerFunc簽名,而是直接返回它,我們通過這種方式來注入狀態。在main.go中,我們定義了兩個路由,其中UseDefault設置為false,另一個路由設置為true。這可以在定義跨越多個路由的函數時使用,也可以在使用處理程序感覺過于繁瑣的結構時使用。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 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>

                              哎呀哎呀视频在线观看