From 2132de1a0848794a49225470e42f91e9c34bb6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Thu, 14 Jun 2018 17:00:00 +0200 Subject: [PATCH] Extract WriteTo method (see io.WriterTo) to expose bytes written Extract a WriteTo() method (see io.WriterTo) that exposes the count of bytes written and rewrite Write() to use it. --- file.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/file.go b/file.go index 39798e1..4f06d1e 100644 --- a/file.go +++ b/file.go @@ -60,6 +60,12 @@ func (f *File) SaveAs(name string) error { // Write provides function to write to an io.Writer. func (f *File) Write(w io.Writer) error { + _, err := f.WriteTo(w) + return err +} + +// WriteTo implements io.WriterTo to write the file. +func (f *File) WriteTo(w io.Writer) (int64, error) { buf := new(bytes.Buffer) zw := zip.NewWriter(buf) f.contentTypesWriter() @@ -70,21 +76,17 @@ func (f *File) Write(w io.Writer) error { for path, content := range f.XLSX { fi, err := zw.Create(path) if err != nil { - return err + return 0, err } _, err = fi.Write(content) if err != nil { - return err + return 0, err } } err := zw.Close() if err != nil { - return err + return 0, err } - if _, err := buf.WriteTo(w); err != nil { - return err - } - - return nil + return buf.WriteTo(w) }