上QQ阅读APP看书,第一时间看更新
How to do it...
- Open the console and create the folder chapter02/recipe04.
- Navigate to the directory.
- Create the concat_buffer.go file with the following content:
package main
import (
"bytes"
"fmt"
)
func main() {
strings := []string{"This ", "is ", "even ",
"more ", "performant "}
buffer := bytes.Buffer{}
for _, val := range strings {
buffer.WriteString(val)
}
fmt.Println(buffer.String())
}
- Run the code by executing go run concat_buffer.go.
- See the output in the Terminal:
- Create the concat_copy.go file with the following content:
package main
import (
"fmt"
)
func main() {
strings := []string{"This ", "is ", "even ",
"more ", "performant "}
bs := make([]byte, 100)
bl := 0
for _, val := range strings {
bl += copy(bs[bl:], []byte(val))
}
fmt.Println(string(bs[:]))
}
- Run the code by executing go run concat_copy.go.
- See the output in the Terminal: