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
+6.9k Golang : Get expvar(export variables) to work with multiplexer
+28.3k Golang : Connect to database (MySQL/MariaDB) server
+14.8k Golang : Reset buffer example
+12.5k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+5.8k Javascript : How to refresh page with JQuery ?
+8.4k Prevent Write failed: Broken pipe problem during ssh session with screen command
+6.3k Linux/Unix : Commands that you need to be careful about
+30.5k Golang : How to verify uploaded file is image or allowed file types
+8.3k Golang : Qt splash screen with delay example
+14.1k Generate salted password with OpenSSL example
+5k Javascript : How to get width and height of a div?
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter