fmt.Fprint
instead of (io.Writer).Write
along with fmt.Sprint
GO-P4007It 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)
}