0
  1. fmt.Sprintf("template with %s %s %s... placeholders", str1, str2 str3, ...)
  2. fmt.Fprintf() instead of Sprintf()
  3. str1 + str2 + str3 + ...
  4. using a strings.Builder's WriteString() on the string pieces, and then return the result from the String() method

I am writing email service that generates SMTP messages which contains long chunks of Base64 encoded file contents, and involves lots of text concatenation to form headers. On high traffics, I try to avoid creating copies of Base64 file contents.

I wanna know if strings.Builder clearly wins over the other two approaches in terms of the efficiency of memory use and time taken.

Eric L.
  • 187
  • 1
  • 3
  • 10
  • Have you tried Google? I remember seeing more than one article specifically on this topic some time ago. – mkopriva Apr 02 '21 at 04:23
  • 2
    Most likely you should be taking advantage of the writer interface and continuously writing your string pieces to the connection. There's no sense in building up a big string in memory when you can just stream the pieces one by one to the end destination. See https://golang.org/pkg/net/smtp/#Client.Data – Hymns For Disco Apr 02 '21 at 04:25
  • If you're interested there is also this https://stackoverflow.com/questions/1760757/how-to-efficiently-concatenate-strings-in-go/1766304 – mkopriva Apr 02 '21 at 04:25

0 Answers0