亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang io/ioutil NopCloser

Golang io/ioutil NopCloser

Go
桃花長相依 2021-09-13 10:53:24
有沒有人對 Golang 的NopCloser功能有很好的解釋?我環顧四周,但除了 Golang 的主要文檔對以下內容的解釋外,沒有找到任何內容:NopCloser 返回一個 ReadCloser,其中包含一個封裝了提供的 Reader r 的無操作 Close 方法。任何指針或解釋將不勝感激。謝謝。
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

每當您需要返回 an 時io.ReadCloser,同時確保 aClose()可用,您可以使用 aNopCloser來構建這樣的 ReaderCloser。


你可以在gorest 的這個分支中看到一個例子,在util.go


//Marshals the data in interface i into a byte slice, using the Marhaller/Unmarshaller specified in mime.

//The Marhaller/Unmarshaller must have been registered before using gorest.RegisterMarshaller

func InterfaceToBytes(i interface{}, mime string) (io.ReadCloser, error) {

    v := reflect.ValueOf(i)

    if v.Kind() == reflect.Ptr {

        v = v.Elem()

    }

    switch v.Kind() {

    case reflect.Bool:

        x := v.Bool()

        if x {

            return ioutil.NopCloser(bytes.NewBuffer([]byte("true"))), nil

        }


查看完整回答
反對 回復 2021-09-13
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

它用于需要io.ReadCloser但當前對象(例如 a bytes.Buffer)不提供 Close 函數的函數。


查看完整回答
反對 回復 2021-09-13
  • 3 回答
  • 0 關注
  • 641 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號