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
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+13.3k Golang : Query string with space symbol %20 in between
+16.2k Golang : Send email and SMTP configuration example
+7.9k Golang : Get final or effective URL with Request.URL example
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+21.4k Golang : Convert string slice to struct and access with reflect example
+12.1k Golang : Validate email address
+11.2k Golang : Generate DSA private, public key and PEM files example
+30.1k Get client IP Address in Go
+5.2k Javascript : How to loop over and parse JSON data?
+29.5k Golang : Get and Set User-Agent examples
+16.5k Golang : Get the IPv4 and IPv6 addresses for a specific network interface