作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
有没有人对 Golang 的 NopCloser
有好的或任何解释?功能?
我环顾四周,但除了 Golang 的主要文档对以下内容的解释外,没有找到任何东西:
NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.
任何指针或解释将不胜感激。谢谢。
最佳答案
当您需要返回 io.ReadCloser
时,在确保 Close()
可用的同时,您可以使用 NopCloser
来构建这样的 ReaderCloser。
您可以在 fork of 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
}
关于Golang io/ioutil NopCloser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158990/
有没有人对 Golang 的 NopCloser 有好的或任何解释?功能? 我环顾四周,但除了 Golang 的主要文档对以下内容的解释外,没有找到任何东西: NopCloser returns a
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我是一名优秀的程序员,十分优秀!