Last active
September 18, 2024 17:30
-
-
Save koonix/447c7aaf3bd6be90d2f2d365813b2718 to your computer and use it in GitHub Desktop.
Go package that adds Close to Reader/Writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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