Whenever you need to return io.ReadCloser , make sure Close() is available, you can use NopCloser to create such a ReaderCloser.
You can see one example in this gorest fork , in 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 }
source share