Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: add StringWriter interface type #27946

Closed
mvdan opened this issue Sep 30, 2018 · 2 comments
Closed

io: add StringWriter interface type #27946

mvdan opened this issue Sep 30, 2018 · 2 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Proposal Proposal-Accepted
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Sep 30, 2018

I propose that we export this interface:

// stringWriter is the interface that wraps the WriteString method.
type stringWriter interface {
        WriteString(s string) (n int, err error)
}

Strictly speaking, this is not necessary. One can declare it elsewhere, or use it inline such as w.(interface{ WriteString(string) (int, error) }).

However, I still think we should export it for a number of reasons:

  • Consistency. We export Writer and ByteWriter already, so I've expected io.StringWriter to exist many times in the past.
  • Better documentation. For example, If w implements a WriteString method in the godoc can be a bit confusing if one is not used to that interface and signature. If w implements StringWriter is much clearer.
  • Deduplication. In the standard library alone, the interface is copy-pasted at least five times, and I've seen it countless times elsewhere:
$ git grep 'type.*string.*interface {$'
fmt/fmt_test.go:type stringWriter interface {
io/io.go:type stringWriter interface {
net/http/h2_bundle.go:type http2stringWriter interface {
net/http/serve_test.go:         type stringWriter interface {
runtime/error.go:type stringer interface {
strings/replace.go:type stringWriterIface interface {

None of these points is a dealbreaker, but they do make learning and dealing with writers clunkier than it should be.

@dvyukov suggested exporting the interface in #21718, along with adding a WriteByte function. Note, however, that the issue was closed because it was decided that the function wasn't necessary. I agree with the decision. However, the interface should be considered separately.

@gopherbot gopherbot added this to the Proposal milestone Sep 30, 2018
@rsc
Copy link
Contributor

rsc commented Oct 3, 2018

OK.

@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Oct 3, 2018
@rsc rsc changed the title proposal: io: add StringWriter interface type io: add StringWriter interface type Oct 3, 2018
@rsc rsc modified the milestones: Proposal, Unplanned Oct 3, 2018
@mvdan mvdan self-assigned this Oct 3, 2018
@gopherbot
Copy link

Change https://golang.org/cl/139457 mentions this issue: io: export StringWriter

@golang golang locked and limited conversation to collaborators Oct 3, 2019
@rsc rsc unassigned mvdan Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Proposal Proposal-Accepted
Projects
None yet
Development

No branches or pull requests

4 participants