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 Javascript : Get operating system and browser information
+5k Golang : How to write backslash in string?
+13.7k Golang : Basic authentication with .htpasswd file
+10k Golang : How to use if, eq and print properly in html template
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+6.5k Golang : Command line ticker to show work in progress
+4.4k Golang : Generate Interleaved 2 inch by 5 inch barcode
+5.1k PageSpeed : Clear or flush cache on web server
+13.2k Golang : How to shuffle elements in array or slice?
+19.8k Golang : How to get time zone and load different time zone?
+4.5k Golang : fmt.Println prints out empty data from struct
+4.8k Golang : Denco multiplexer example