这更多是与接口/功能/闭合/类型/ ...有关的通用Go问题,但具有执行I / O的具体用例。
给出一个io.Reader
变量,我想使用自定义io.ReadCloser
实现创建一个Close()
。
var file os.File
file = ... // some File which needs to be closed
var reader io.Reader
reader = ... // get Reader from somewhere
var readCloser io.ReadCloser
readCloser = ... // how to create a ReadCloser here?
在Java中,我会写类似的东西:
readCloser = new io.ReadCloser() {
Read(p []byte) {
reader.Read(p);
}
Close() {
// my custom close implementation
file.Close()
}
}
// now pass readCloser around as needed
如何使用Go做到这一点?
答案 0 :(得分:2)
一种方法是创建自己的类型,将io.Reader
与
io.Closer
并实现io.ReadCloser
接口。像这样:
type ReadCloseCombiner struct {
io.Reader
io.Closer
}
func (rcc ReadCloseCombiner) Read(p []byte) (n int, err error) {
return rcc.Reader.Read(p)
}
func (rcc ReadCloseCombiner) Close() error {
return rcc.Closer.Close()
}
并像这样使用它:
var file *os.File
file = ... // some File which needs to be closed
var reader io.Reader
reader = ... // get Reader from somewhere
var readCloser io.ReadCloser
readCloser = ReadCloseCombiner{reader, file}
// now pass readCloser around as needed
readCloser.Read(...)
readCloser.Close()
如果您想要更灵活的东西,我会考虑使用具有读取和关闭功能的类型,然后可以将其传递给匿名函数(在您的情况下甚至是reader.Read
/ file.Close
)。
答案 1 :(得分:2)
从http.HandlerFunc
得到提示,我想到了以下
package main
import (
"bytes"
"fmt"
"io"
"strings"
)
// ReadData reads data from an io.Reader
func ReadData(r io.Reader) {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
s := buf.String()
fmt.Printf("read : %s \n", s)
}
// ReaderFunc takes a function and returns io.Reader
type ReaderFunc func([]byte) (int, error)
func (r ReaderFunc) Read(b []byte) (int, error) {
return r(b)
}
func main() {
// invoking ReadData with a regular io.Reader
r := strings.NewReader("data 123")
ReadData(r)
// invoking ReadData with an anonymous io.Reader
r = strings.NewReader("data 456")
ReadData(ReaderFunc(func(b []byte) (int, error) {
return r.Read(b)
}))
}