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 Golang : Serving HTTP and Websocket from different ports in a program example
+14.6k Golang : Read large file with bufio.Scanner cause token too long error
+4.7k Swift : Get substring with rangeOfString() function example
+4.4k Unix/Linux : How to archive and compress entire directory ?
+23.5k Golang : Change file read or write permission example
+8.3k Golang : Terminate-stay-resident or daemonize your program?
+13.6k Golang : Execute function at intervals or after some delay
+5.7k Golang : Gargish-English language translator
+9.4k Golang : Generate random integer or float number
+5.9k How to let Facebook Login button redirect to a particular URL ?
+7.2k Golang : Check if integer is power of four example
+6.9k Golang : get the current working directory of a running program