This is more of a generic Go question related to interfaces/functions/closures/types/..., but with a concrete use case of performing I/O.
Given an io.Reader
variable I want to create an io.ReadCloser
with a custom Close()
implementation.
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?
In Java, I would write something like:
readCloser = new io.ReadCloser() {
Read(p []byte) {
reader.Read(p);
}
Close() {
// my custom close implementation
file.Close()
}
}
// now pass readCloser around as needed
How to do that with Go?