3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
每當(dāng)您需要返回 an 時(shí)io.ReadCloser,同時(shí)確保 aClose()可用,您可以使用 aNopCloser來構(gòu)建這樣的 ReaderCloser。
你可以在gorest 的這個(gè)分支中看到一個(gè)例子,在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
}

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
它用于需要io.ReadCloser
但當(dāng)前對(duì)象(例如 a bytes.Buffer
)不提供 Close 函數(shù)的函數(shù)。
- 3 回答
- 0 關(guān)注
- 634 瀏覽
添加回答
舉報(bào)