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.1k Golang : Convert file content to Hex
+12.3k Golang : How to tell if a file is compressed either gzip or zip ?
+13.6k Golang : Convert IPv4 address to decimal number(base 10) or integer
+35.8k Golang : Read tab delimited file with encoding/csv package
+2.9k Which content-type(MIME type) to use for JSON data
+10.1k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+5.8k Swift : Convert (cast) String to Double
+4.8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+18.3k Golang : Time slice or date sort and reverse sort example
+5.5k Golang : Multiplexer with net/http and map
+15.9k Golang : Create and resolve(read) symbolic links