fmt.Fprint
instead of (io.Writer).Write
along with fmt.Sprint
GO-P40071487 ),
1488 stdoutStream("foo-handle", "process-handle", 123, func(conn net.Conn) {
1489 conn.Write([]byte("stdout data"))
1490 conn.Write([]byte(fmt.Sprintf("roundtripped %s", <-expectedRoundtrip)))1491 }),
1492 stderrStream("foo-handle", "process-handle", 123, func(conn net.Conn) {
1493 conn.Write([]byte("stderr data"))
1134 ),
1135 stdoutStream("foo-handle", "process-handle", 123, func(conn net.Conn) {
1136 conn.Write([]byte("stdout data"))
1137 conn.Write([]byte(fmt.Sprintf("roundtripped %s", <-stdInContent)))1138 }),
1139 emptyStderrStream("foo-handle", "process-handle", 123),
1140 )
1011 ),
1012 stdoutStream("foo-handle", "process-handle", 123, func(conn net.Conn) {
1013 conn.Write([]byte("stdout data"))
1014 conn.Write([]byte(fmt.Sprintf("roundtripped %s", <-stdInContent)))1015 }),
1016 stderrStream("foo-handle", "process-handle", 123, func(conn net.Conn) {
1017 conn.Write([]byte("stderr data"))
It is recommended to use fmt.Fprint
(and friends) instead of writing the
result of an fmt.Sprint
(and friends) call to write to an io.Writer
. This
reduces the number of allocations required, therefore improving performance.
package main
import (
"fmt"
"io"
)
func foo(w io.Writer, a int) {
w.Write([]byte(fmt.Sprintf("A: %d", a)))
}
package main
import (
"fmt"
"io"
)
func foo(w io.Writer, a int) {
fmt.Fprintf(w, "A: %d", a)
}