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
+10.1k CodeIgniter : Load different view for mobile devices
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+9.3k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+8.2k Golang : Multiplexer with net/http and map
+20.6k nginx: [emerg] unknown directive "passenger_enabled"
+9.9k Golang : interface - when and where to use examples
+12.2k Golang : Perform sanity checks on filename example
+6.1k Fontello : How to load and use fonts?
+20k Golang : How to get time from unix nano example
+7.1k Golang : Takes a plural word and makes it singular
+16.4k Golang : Find out mime type from bytes in buffer
+10.4k Golang : How to check if a website is served via HTTPS