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?
taking a hint from http.HandlerFunc
, i could think of the following
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
", 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)
}))
}
One way could be to create your own type that combines a io.Reader
and a io.Closer
and implements the io.ReadCloser
interface. Something like this:
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()
}
And use it like this:
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()
If you want something more flexible i would concider having a type that take a read and close function instead and then you could pass it anonymous functions (or even reader.Read
/file.Close
in your case).