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
+12.9k Golang : Get terminal width and height example
+15.4k Golang : invalid character ',' looking for beginning of value
+21.9k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+10k Golang : Identifying Golang HTTP client request
+13.7k Golang : Gin framework accept query string by post request example
+4.6k JavaScript : Rounding number to decimal formats to display currency
+21.6k SSL : How to check if current certificate is sha1 or sha2
+9.6k Golang : Find correlation coefficient example
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+16.4k Golang : How to implement two-factor authentication?
+40.9k Golang : How to check if a string contains another sub-string?