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.1k Random number generation with crypto/rand in Go
+9.8k Golang : Natural string sorting example
+5.2k Golang : Create new color from command line parameters
+22.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+12.9k Golang : Get uploaded file name or access uploaded files
+35.6k Upload multiple files with Go
+8.4k Facebook : Getting the friends list with PHP return JSON format
+27.8k Golang : Get first few and last few characters from string
+4.8k Linux : Disable and enable IPv4 forwarding
+16.1k Golang : Clone with pointer and modify value
+15.1k Golang : Test floating point numbers not-a-number and infinite example
+10.5k Golang : Convert decimal number(integer) to IPv4 address