You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
io provides WriteString function and ByteWriter interface. Is there any reason for this asymmetry? I think it should also provide WriteByte function and StringWriter interface. Or, at least, WriteByte function, which is a handy short-cut and avoids byte slice allocation:
func WriteByte(w Writer, b byte) (n int, err error) {
if bw, ok := w.(io.ByteWriter); ok {
return bw.WriteByte(b)
}
return w.Write([]byte{b})
}
The text was updated successfully, but these errors were encountered:
cespare
changed the title
io: add WriteByte/StringWriter assymetry
io: add WriteByte/StringWriter for symmetry
Aug 31, 2017
There is stringWriter interface already in the io package, but it is not exported. It would be useful in some cases. For example: Currently, the net/http.ResponseWriter interface is implemented by *net/http.response, which is also implements io.stringWriter (http.response already has WriteString method). But there is no WriteString in net/http.ResponseWriter interface. So, you should call io.WriteString(w, s) instead of w.WriteString(s) every time when you need to write the small data chunk into w. It would be better to cast http.ResponseWriter to io.stringWriter only once instead of doing this every time you call io.WriteString (but specifically in this case it would be better to add WriteString into http.ResponseWriter).
io
providesWriteString
function andByteWriter
interface. Is there any reason for this asymmetry? I think it should also provideWriteByte
function andStringWriter
interface. Or, at least,WriteByte
function, which is a handy short-cut and avoids byte slice allocation:The text was updated successfully, but these errors were encountered: