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
+5.1k Golang : Extract XML attribute data with attr field tag example
+12.4k Golang : Query string with space symbol %20 in between
+24.8k Golang : Convert(cast) string to uint8 type and back to string
+6.2k Golang : How to handle file size larger than available memory panic issue
+8.8k Golang : Check a web page existence with HEAD request example
+6.1k Web : How to see your website from different countries?
+17.9k Golang : How to get own program name during runtime ?
+13.5k Golang : Execute function at intervals or after some delay
+10.3k Golang : Display a text file line by line with line number example
+16.1k Golang : Qt image viewer example
+6.6k Gogland : Single File versus Go Application Run Configurations
+3.8k Linux/MacOSX : How to symlink a file?