mirror of
https://github.com/golang/go
synced 2024-11-21 23:44:39 -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:
parent
546c78b744
commit
cf3eeb2984
@ -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))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user