underlying changes, I needed the caller of that method to "close" the tar
file when through with the returned *tar.Reader. However, *tar.Reader does
not have a Close method.
So I changed my method, which used to return *tar.Reader, to return
TarReadCloser, a new type in my package, defined thus:
// A TarReadCloser is a tar.Reader which also implements Close.
type TarReadCloser struct {
*tar.Reader
io.Closer
}
The implementation of my method used to say something like this:
return tar.NewReader(r)
and now it does this:
return &TarReadCloser{Reader: tar.NewReader(r), Closer: r}
This is a striking elegance; far better than what you have to do without
the wonders of anonymous fields and method promotion. For example, without
method promotion, I'd need to define methods on TarReadCloser,
corresponding to all the methods of *tar.Reader, and then I'd need to keep
that up-to-date with any changes to the archive/tar package. Instead, the
compiler will do that for me through this relatively simple expedient.
Thomas
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.