qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.62k stars 1.68k forks source link

excelize.File does not implement io.WriterTo #1901

Open fightlight opened 1 month ago

fightlight commented 1 month ago

Description

Hi! I'm migrating one of our projects from excelize v2.4.0 to v2.8.1 and can I not compile code:

cannot use xlsx (variable of type *excelize.File) as io.WriterTo value in return statement: *excelize.File does not implement io.WriterTo (wrong type for method WriteTo)
                have WriteTo(io.Writer, ...excelize.Options) (int64, error)
                want WriteTo(io.Writer) (int64, error)

I made a workaround returning *excelize.File instead of io.WriterTo, but anyway the issue (or the go doc comment) should be fixed, I think :)

Steps to reproduce the issue:

  1. Create a method returning excelize.NewFile()
  2. Make the return type as io.WriterTo
  3. go build

Describe the results you received: Code doesn't compile.

Describe the results you expected: Code compiles.

Output of go version:

go version go1.22.2 darwin/amd64

Excelize version or commit ID:

v2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.): MacOS Ventura 13.6.6

dolmen commented 1 month ago

It is very unfortunate that fb1aab7add52808c96c9cc10570fe73ce797b7f4 broke compatibilty with io.WriterTo (I was the one who introduced it in 2132de1a0848794a49225470e42f91e9c34bb6da) since v2.7.0.

However, at that point restoring the compatibility would be another another breaking change. So I don't think this is fixable. Except the documentation, of course.

dolmen commented 1 month ago

Note that, as a workaround, it is easy to write your own wrapper to somewhat restore that compatibility:

type myExcelizeFile = excelize.File

type myExcelizeWriterTo struct {
    *myExcelizeFile
}

func (f myExcelizeWriterTo) WriteTo(w io.Writer) (int64, error) {
    return f.myExcelizeFile.WriteTo(w)
}