returning struct vs. returning interface

111 views
Skip to first unread message

Jochen Voss

unread,
Aug 3, 2022, 12:09:58 PM8/3/22
to golang-nuts
Dear all,

Why does the function NewReader [1] in compress/lzw return an io.ReadCloser instead of an *lzw.Reader?  The docs say "It is guaranteed that the underlying type of the returned io.ReadCloser is a *Reader", so why not return a *Reader without wrapping?


Many thanks,
Jochen

Axel Wagner

unread,
Aug 3, 2022, 12:34:13 PM8/3/22
to Jochen Voss, golang-nuts
git blame points at
That is: Originally, the type returned by NewReader was unexported, so an interface was used. Then, methods where added to that type which would be useful to be used, so the type needed to be exported. The signature of NewReader could not change, though, for backwards compatibility. Documenting that it's guarenteed to be the newly exported type makes it possible to use the new methods without breaking compatibility.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7ee4994a-a9e2-44d7-a939-9ca9fa7529d4n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages