func Append(slice, data[]byte) []byte {l := len(slice);if l + len(data) > cap(slice) { // reallocate// Allocate double what's needed, for future growth.newSlice := make([]byte, (l+len(data))*2);// Copy data (could use bytes.Copy()).for i, c := range slice {newSlice[i] = c}slice = newSlice;}slice = slice[0:l+len(data)];for i, c := range data {slice[l+i] = c}return slice;}
package main
import ("bytes""fmt")
func main() {var buffer bytes.Buffer
for i := 0; i < 1000; i++ {buffer.WriteString("a")}
fmt.Println(buffer.String())}
package main
import ("strings""fmt")
func main() {// ZERO-VALUE://// It's ready to use from the get-go.// You don't need to initialize it.var sb strings.Builder
for i := 0; i < 1000; i++ {sb.WriteString("a")}
fmt.Println(sb.String())}
package main
import ("fmt""strings")
func main (){concatenation:= strings.Join([]string{"a","b","c"},"") //where second parameter is a separator.fmt.Println(concatenation) //abc}
package main
import ("fmt")
var N int = 100000
func main() {slice1 := make([]rune, N, N)//Efficient with fast performance, Need pre-allocated memory//We can add a check if we reached the limit then increase capacity//using append, but would be fined for data copying to new array. Also append happens after the length of current slice.for i := 0; i < N; i++ {copy(slice1[i:i+1], []rune{'N'})}fmt.Println(slice1)
//Simple but fast solution, Every time the slice capacity is reached we get a fine of effort that goes//in copying data to new arrayslice2 := []rune{}for i := 0; i <= N; i++ {slice2 = append(slice2, 'N')}fmt.Println(slice2)
}