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
}
- 3 回答
- 0 關注
- 641 瀏覽
添加回答
舉報