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
+7.3k Golang : Scanf function weird error in Windows
+9k Golang : Capture text return from exec function example
+9.9k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+10.5k Golang : Get local time and equivalent time in different time zone
+21.1k Golang : Create and resolve(read) symbolic links
+21.3k Curl usage examples with Golang
+25.6k Golang : How to write CSV data to file
+36.3k Golang : Save image to PNG, JPEG or GIF format.
+31.8k Golang : Convert an image file to []byte
+7.7k Golang : Generate human readable password
+8.1k Golang : Qt splash screen with delay example
+12.3k Golang : Search and extract certain XML data example