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 关注
- 661 浏览
添加回答
举报