1
0
mirror of https://github.com/golang/go synced 2024-11-21 17:44:40 -07:00

io.WriteString: if the object has a WriteString method, use it

This avoids allocation when writing to bytes.Buffers and bufio.Writers, for
example.

R=golang-dev, rsc, r, consalus, r
CC=golang-dev
https://golang.org/cl/4625068
This commit is contained in:
Evan Shaw 2011-06-28 16:10:39 +10:00 committed by Rob Pike
parent 546c78b744
commit cf3eeb2984

View File

@ -209,8 +209,16 @@ type RuneScanner interface {
UnreadRune() os.Error
}
// stringWriter is the interface that wraps the WriteString method.
type stringWriter interface {
WriteString(s string) (n int, err os.Error)
}
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
func WriteString(w Writer, s string) (n int, err os.Error) {
if sw, ok := w.(stringWriter); ok {
return sw.WriteString(s)
}
return w.Write([]byte(s))
}