Skip to content

Instantly share code, notes, and snippets.

@koonix
Last active September 18, 2024 17:30
Show Gist options
  • Save koonix/447c7aaf3bd6be90d2f2d365813b2718 to your computer and use it in GitHub Desktop.
Save koonix/447c7aaf3bd6be90d2f2d365813b2718 to your computer and use it in GitHub Desktop.
Go package that adds Close to Reader/Writer
package withcloser
import "io"
type reader struct {
io.Reader
close func() error
}
func (r reader) Close() error {
return r.close()
}
func Reader(r io.Reader, close func() error) io.ReadCloser {
return reader{
Reader: r,
close: close,
}
}
package withcloser
import "io"
type writer struct {
io.Writer
close func() error
}
func (w writer) Close() error {
return w.close()
}
func Writer(w io.Writer, close func() error) io.WriteCloser {
return writer{
Writer: w,
close: close,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment