<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【GoCN酷Go推薦】靈活的Go http client庫-Sling

          共 2536字,需瀏覽 6分鐘

           ·

          2021-06-02 23:46

          推薦理由

          目開發(fā)中,發(fā)送http請求的場景,推薦使用Sling庫。Sling本身是基于net/http來處理發(fā)送請求,同時做了較好的封裝,既可以利用net/http的一些特性(如:httptrace),同時又不必關(guān)心net/http庫的一些瑣碎細節(jié)。

          功能介紹

          Sling支持以下主要的功能:
          • 支持GET/POST/PUT/PATCH/DELETE/HEAD
          • 基于Base/Path可以擴展和復用Sling
          • query參數(shù)可以用結(jié)構(gòu)體來Encode
          • Request Body支持form和json
          • 可將Json格式的Response直接Decode到定義好的結(jié)構(gòu)體
          • 可擴展Response的Decoder,以及Doer。

          使用指南


          1. Sling對http請求的要素method、baseUrl、Path、query、body、request、response等做了封裝,基本使用可以參考https://github.com/dghubble/sling 上的示例代碼。

          2. 可以通過實現(xiàn)ResponseDecoder和Doer的接口,來定制響應(yīng)的decoder和發(fā)送請求的具體實現(xiàn)。

          3. 利用Sling靈活擴展的特性提高業(yè)務(wù)代碼復用,可以參考下面的示例:

          const baseURL = "https://api.github.com/"

          // Github Issue (abbreviated)
          type Issue struct {
              Title  string `json:"title"`
              Body   string `json:"body"`
          }

          type IssueService struct {
              sling *sling.Sling
          }

          func NewIssueService(httpClient *http.Client) *IssueService {
              return &IssueService{
                  sling: sling.New().Client(httpClient).Base(baseURL),
              }
          }

          func (s *IssueService) ListByRepo(owner, repo string, params *IssueListParams) ([]Issue, *http.Response, error) {
              issues := new([]Issue)
              githubError := new(GithubError)
              path := fmt.Sprintf("repos/%s/%s/issues", owner, repo)
              // 注意此處一定要調(diào)用New方法來clone一個sling實例
              resp, err := s.sling.New().Get(path).QueryStruct(params).Receive(issues, githubError)
              if err == nil {
                  err = githubError
              }
              return *issues, resp, err
          }

          總結(jié)


          Sling的默認實現(xiàn)可以覆蓋大部分對于http發(fā)送請求場景,同時可以通過實現(xiàn)Doer和ResponseDecoder接口來擴展個性化的場景。

          參考資料


          • https://github.com/dghubble/sling


          還想了解更多嗎?

          更多請查看:https://github.com/dghubble/sling

          歡迎加入我們GOLANG中國社區(qū):https://gocn.vip/


          《酷Go推薦》招募:


          各位Gopher同學,最近我們社區(qū)打算推出一個類似GoCN每日新聞的新欄目《酷Go推薦》,主要是每周推薦一個庫或者好的項目,然后寫一點這個庫使用方法或者優(yōu)點之類的,這樣可以真正的幫助到大家能夠?qū)W習到

          新的庫,并且知道怎么用。


          大概規(guī)則和每日新聞類似,如果報名人多的話每個人一個月輪到一次,歡迎大家報名!(報名地址:https://wj.qq.com/s2/7734329/3f51)


          掃碼也可以加入 GoCN 的大家族喲~


           Gopher China2021大會日程詳情來了!


          點擊閱讀原文,還有機會獲得門票參加 Gopher China 大會哦~

          瀏覽 132
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大鸡吧av网站 | 欧美日韩成人在线 | 91视频偷拍 | 久久久久成人无码 | 大香蕉国产伊人在线 |