Hacker News new | ask | show | jobs
by rjempson 4721 days ago
I am just wondering whether you can write your own wrapper class that implements (exposes) the read method and doesn't expose a close method (or expose a close method that does nothing) and pass that in?

I am not suggesting this is a good solution, just curious.

1 comments

Yes, easily:

    var rc io.ReadCloser // a file, whatever
    readerOnly := struct{io.Reader}{rc} // only a Reader
That's cool. The io.Reader is an unnamed field, so its method is effectively pushed to the surface of the wrapping struct instead of needing to be explicitly forwarded to.