Common Stitching Method
String stitching is a common requirement in daily development, and there are two common approaches:
One is directly using + = to Splice
" Hello " S2: " World " s3:= s1 + s2 // s3 = "HelloWorld"s1 + = s2 // S1 = = "HelloWorld" /c14>
This is the most common and simple intuitive method, but the simple is a price, Golang string is immutable type, that is, each time the "in situ" modification of the string will regenerate a string, and then copy the data into it, which will produce a considerable performance overhead, You will see this in a later performance test.
The second is the use of bytes. Buffer
// bytes. The 0 value of buffer can be used directly with the var buff bytes. Buffer // writes the character/string buff to the buff. Write ([]byte ( " hello " " ' ) Buff. WriteString ( " world " ) // string () method gets the concatenation of the string buff. String () // "Hello World"
This method is much better than the first method for applications that require a lot of string concatenation.
But using the bytes module to manipulate a string is confusing, so a third method is added in go1.10: Strings.builder, which is officially encouraged to use the buffer when using builder,byte splicing as much as possible in string concatenation
//strings. Builder's 0 value can be used directlyvarBuilder strings. Builder//writing characters/strings to builderBuilder. Write ([]byte("Hello") ) builder. WriteByte (' ') Builder. WriteString (" World")//string () method to get stitched stringsBuilder. String ()//"Hello World"
As you can see from the code above, strings. Builder and Bytes.buffer operate almost the same way, but Strings.builder only implements the Write class method, and buffer is readable and writable.
So strings. Builder only for stitching/building strings
Performance
In addition to ease of use, another reference standard is performance, thanks to Golang's own test tools, we can roughly compare the performance of three scenarios.
The test uses a total of 67 characters from 26 uppercase and lowercase letters 10 digits and 5 commonly used symbols to randomly take 10 constituent strings or []byte, which are then spliced by buffer and builder.
Test results First
Go test-bench=. -benchmem
Here is the test code
//BenchmarkSpliceAddString10 Test Using + = splicing N-Times length 10 stringFunc BenchmarkSpliceAddString10 (b *testing. B) {s:="" forI: =0; i < B.N; i++{s+ = Genrandstring (Ten) }}//The BENCHMARKSPLICEBUILDERSTRING10 test uses strings. Builder Stitching N-times a string of length 10Func BenchmarkSpliceBuilderString10 (b *testing. B) {varBuilder strings. Builder forI: =0; i < B.N; i++{Builder. WriteString (Genrandstring (Ten)) }}//The BenchmarkSpliceBufferString10 test uses bytes. Buffer splicing N-times a string of length 10Func BenchmarkSpliceBufferString10 (b *testing. B) {varBuff bytes. Buffer forI: =0; i < B.N; i++{buff. WriteString (Genrandstring (Ten)) }}//The BenchmarkSpliceBufferByte10 test uses bytes. Buffer splicing n times the length of 10 []byteFunc BenchmarkSpliceBufferByte10 (b *testing. B) {varBuff bytes. Buffer forI: =0; i < B.N; i++{buff. Write (Genrandbytes (Ten)) }}//The BenchmarkSpliceBuilderByte10 test uses a string. Builder splicing n times the length of 10 []byteFunc BenchmarkSpliceBuilderByte10 (b *testing. B) {varBuilder strings. Builder forI: =0; i < B.N; i++{Builder. Write (Genrandbytes (Ten)) }}
This is the code that generates a random string to use for stitching (bytes is still used here. Buffer, it is recommended to use the new strings. Builder)
Const(Data="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.-=/") Func init () {rand. Seed (time. Now (). Unix ())//set Random seed}//genrandstring generates n random characters of stringFunc genrandstring (nint)string{max:=len (data)varbuf bytes. Buffer forI: =0; I < n; i++{buf. WriteByte (Data[rand. INTN (max)])}returnbuf. String ()}//genrandbytes generates n random characters for the []byteFunc genrandbytes (nint) []byte{max:=len (data) BUF:= Make ([]byte, N) forI: =0; I < n; i++{Buf[i]=Data[rand. INTN (max)]}returnBUF}
The method of using + = performance is the slowest, performance and the other two tanesashi several orders of magnitude.
Buffer and builder performance is similar, builder in the memory of the use of a slightly better than buffer
Conclusion
Strings. Builder introduced the standard library in Golang 1.10, so version <= 1.9 recommended bytes for a large number of string concatenation operations. Buffer
If you are using 1.10+, it is recommended to use strings. Builder, not only for better performance, but also to make the code clearer.
Of course, for simple splicing, + = is enough