Golang : Concatenate (combine) buffer data example
At previous tutorial on how to concatenate ( combine ) strings data, a reader asked how to concatenate data inside a buffer.
In Golang, the +=
symbol equals to x = x + somethingnew
and we will use the +=
to combine the buffer data(which is converted to string type with the built in String()
and strconv.Itoa()
functions.
Here you go :
package main
import (
"bytes"
"fmt"
"strconv"
)
func main() {
var buffer bytes.Buffer
for i := 0; i < 2; i++ {
buffer.WriteString("a")
}
s := buffer.String()
// s is now aa
for i := 0; i < 8; i++ {
s += strconv.Itoa(i)
}
// s is now aa01234567
s += buffer.String()
// s is now aa01234567aa
fmt.Println(s)
}
Output :
aa01234567aa
Happy Coding!
See also : Golang : concatenate(combine) strings
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+30.6k Golang : Remove characters from string example
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+10.5k Golang : Meaning of omitempty in struct's field tag
+7.2k Golang : Transform lisp or spinal case to Pascal case example
+5.9k Cash Flow : 50 days to pay your credit card debt
+7k Golang : constant 20013 overflows byte error message
+16.5k Golang : Convert slice to array
+11.7k Golang : Concurrency and goroutine example
+9.1k Golang : Capture text return from exec function example
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+16.1k Golang : Generate universally unique identifier(UUID) example