Skip to main content
added 5 characters in body
Source Link
Inanc Gumus
  • 28k
  • 10
  • 88
  • 104

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string.

So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it?

The naive way would be:

svar :=s ""string
for i := 0; i < 1000; i++ {
    s += getShortStringFromSomewhere()
}
return s

but that does not seem very efficient.

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string.

So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it?

The naive way would be:

s := ""
for i := 0; i < 1000; i++ {
    s += getShortStringFromSomewhere()
}
return s

but that does not seem very efficient.

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string.

So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it?

The naive way would be:

var s string
for i := 0; i < 1000; i++ {
    s += getShortStringFromSomewhere()
}
return s

but that does not seem very efficient.

edited title
Link
Jay
  • 20.2k
  • 38
  • 127
  • 196

How to efficiently concatenate strings in go

edited title
Link
Jonathan Hall
  • 80k
  • 19
  • 160
  • 204

How to efficiently concatenate strings in Go?

Notice removed Reward existing answer by user184968
Bounty Ended with Inanc Gumus's answer chosen by CommunityBot
Notice added Reward existing answer by user184968
Bounty Started worth 250 reputation by CommunityBot
Question Protected by icza
deleted 3 characters in body
Source Link
Salvador Dali
  • 223.4k
  • 151
  • 724
  • 765
Loading
Rollback to Revision 2
Source Link
icza
  • 419.8k
  • 77
  • 1k
  • 896
Loading
removed semicolons as they are not required in go
Source Link
icza
  • 419.8k
  • 77
  • 1k
  • 896
Loading
edited tags
Link
hannson
  • 4.5k
  • 8
  • 42
  • 46
Loading
Source Link
Randy Sugianto 'Yuku'
  • 73.6k
  • 58
  • 186
  • 234
Loading