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
+5.4k Python : Create Whois client or function example
+7.2k Nginx : How to block user agent ?
+11.9k Golang : Verify Linux user password again before executing a program example
+5.5k Golang : Reclaim memory occupied by make() example
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+9.9k Golang : Load ASN1 encoded DSA public key PEM file example
+10.6k Generate Random number with math/rand in Go
+7.7k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+19.3k Mac OSX : Homebrew and Golang
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+5.6k How to check with curl if my website or the asset is gzipped ?
+9k Golang : On lambda, anonymous, inline functions and function literals