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
+7.5k Golang : Count leading or ending zeros(any item of interest) example
+15.6k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+8.3k Golang : Scramble and unscramble text message by randomly replacing words
+8.7k Golang : Resumable upload to Google Drive(RESTful) example
+22.7k Golang : Generate MD5 checksum of a file
+10.2k CodeIgniter : How to check if a session exist in PHP?
+15.7k Golang : Get input from keyboard
+13k Golang : Convert IP version 6 address to integer or decimal number
+9.8k Golang : Simple image viewer with Go-GTK
+6.5k Golang : Get YouTube playlist
+25.6k Golang : dial tcp: too many colons in address
+5.4k Golang : Break string into a slice of characters example