这更像是一个与接口/函数/闭包/类型/...相关的通用 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 做到这一点?
慕后森
暮色呼如
相关分类