Go

Go

Made by DeepSource

Use fmt.Fprint instead of (io.Writer).Write along with fmt.Sprint GO-P4007

Performance
Major
Autofix

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.

Bad practice

package main

import (
  "fmt"
  "io"
)

func foo(w io.Writer, a int) {
  w.Write([]byte(fmt.Sprintf("A: %d", a)))
}

Recommended

package main

import (
  "fmt"
  "io"
)

func foo(w io.Writer, a int) {
  fmt.Fprintf(w, "A: %d", a)
}