每当需要返回时
io.ReadCloser,同时确保
Close()可用,则可以使用
NopCloser来构建这样的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.RegisterMarshallerfunc 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 }


