Go

Go

Made by DeepSource

Use (io.StringWriter).WriteString for writing strings GO-P4008

Performance
Major
Autofix

It is recommended to use (io.StringWriter).WriteString instead of (io.StringWriter).Write or io.WriteString for writing strings as it decreases the number of allocations required, therefore improving performance.

Bad practice

package main

import (
  "io"
)

func foo(w io.StringWriter) {
  w.Write([]byte("foo"))
  io.WriteString(w, "bar")
}

Recommended

package main

import (
  "io"
)

func foo(w io.StringWriter) {
  w.WriteString("foo")
  w.WriteString("bar")
}