From cf3eeb29841d671c63d4d308bf8f5a99906d33d8 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Tue, 28 Jun 2011 16:10:39 +1000 Subject: [PATCH] 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 --- src/pkg/io/io.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pkg/io/io.go b/src/pkg/io/io.go index 790cf94e7c..b879fe5b72 100644 --- a/src/pkg/io/io.go +++ b/src/pkg/io/io.go @@ -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)) } -- 2.48.1