Concatenation vs fmt.Fprintf/Sprintf

719 views
Skip to first unread message

Archos

unread,
Sep 30, 2010, 11:00:13 AM9/30/10
to golang-nuts
I've read in any post that the use of string concatenation performs a
malloc each time.

And like strings are immutable, modifying them wih "+" or "+=" is a
timeconsuming and expensive process.

So would be better to consider using fmt.Fprintf/Sprintf to format the
string (except for a single concatenation somewhere in the code). Is
right?

Evan Shaw

unread,
Sep 30, 2010, 11:04:19 AM9/30/10
to Archos, golang-nuts
On Thu, Sep 30, 2010 at 10:00 AM, Archos <raul...@sent.com> wrote:
> I've read in any post that the use of string concatenation performs a
> malloc each time.

This used to be true, but changed fairly recently due to a
compiler/runtime optimization. Now s1 + s2 + s3 compiles to a single
runtime.concatstring call, which allocates only once.

- Evan

Reply all
Reply to author
Forward
0 new messages