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
+14.6k Golang : How to determine if user agent is a mobile device example
+14.3k Elastic Search : Mapping date format and sort by date
+9.5k Golang : Create unique title slugs example
+7k Golang : Find the shortest line of text example
+26.5k Golang : Get executable name behind process ID example
+38.2k Golang : Read a text file and replace certain words
+13.4k Golang : Linear algebra and matrix calculation example
+26.5k Golang : Convert(cast) string to uint8 type and back to string
+5.7k Golang : Frobnicate or tweaking a string example
+4.7k Javascript : Detect when console is activated and do something about it
+5.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+11k Golang : Sieve of Eratosthenes algorithm