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
+16.6k Golang : Get own process identifier
+19.1k Golang : Delete item from slice based on index/key position
+51.2k Golang : Check if item is in slice/array
+18.6k Unmarshal/Load CSV record into struct in Go
+13.5k Golang : Tutorial on loading GOB and PEM files
+8.5k Golang : Set or add headers for many or different handlers
+9.5k Golang : Sort and reverse sort a slice of floats
+6.2k Golang : How to search a list of records or data structures
+13.1k Golang : Date and Time formatting
+9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+9.1k Golang : How to check if a string with spaces in between is numeric?
+26.1k Golang : Get executable name behind process ID example