英文:
How to iterate over a slice and build one string from the output\
问题
如何迭代一个切片并从输出中构建一个新的字符串,然后在函数中返回该字符串?
例如,如果我想读取一个切片并为每个项添加索引,然后将新的更改放入一个单独的字符串中并在函数中返回它:
func buildString() string {
strSlice := []string{"one", "two", "three"}
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf("%d === %s\n", index, element)
}
return bigString
}
在这个示例中,我们使用range
关键字迭代strSlice
切片,并使用+=
运算符将每个项的索引和元素添加到bigString
字符串中。最后,我们将bigString
作为函数的返回值。
英文:
How would I iterate over a slice and build a new single string from the output, then return that string in a function?
For example if I wanted to read a slice and add an index to each item, then put the new changes in a single string and return it in a function:
func buildString() string {
strSlice := []string{"one", "two", "three"}
for index, element := range strSlice{
fmt.Println(index, "===", element)
bigString := append(strSlice, fmt.Prinln(index, "===", element)
}
return bigString
}
答案1
得分: 1
我完全同意马克的评论,但为了完整起见,以下是你尝试实现的问题的一个可能解决方案:
func buildString(strSlice []string) string {
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf("%d === %s\n", index, element)
}
return bigString
}
func main() {
strSlice := []string{"one", "two", "three"}
result := buildString(strSlice)
fmt.Print(result)
}
但是,请按照马克推荐的参观Go之旅。
英文:
I fully agree with Marc's comment, but just for completeness, the following would be a possible solution to what you are trying to accomplish:
func buildString(strSlice []string) string {
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf("%d === %s\n", index, element)
}
return bigString
}
func main() {
strSlice := []string{"one", "two", "three"}
result := buildString(strSlice)
fmt.Print(result)
}
But please, take the Tour of Go that Marc recommended to you.
答案2
得分: 1
你可以使用strings.Builder
,它更高效。请尝试以下代码:
func buildString(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteRune('\n')
}
return sb.String()
}
基准测试结果如下:
Benchmark1-8 4203799 252 ns/op 56 B/op 3 allocs/op
Benchmark6-8 5044305 280 ns/op 56 B/op 3 allocs/op
Benchmark4-8 4332459 319 ns/op 72 B/op 3 allocs/op
Benchmark5-8 2792514 371 ns/op 56 B/op 3 allocs/op
Benchmark3-8 1000000 1018 ns/op 144 B/op 8 allocs/op
Benchmark2-8 1000000 1109 ns/op 152 B/op 8 allocs/op
代码如下:
package main
import (
"fmt"
"strconv"
"strings"
"testing"
)
func buildString(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteRune('\n')
}
return sb.String()
}
func buildString6(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteString("\n")
}
return sb.String()
}
func buildString5(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === " + s + "\n")
}
return sb.String()
}
func buildString4(a []string) string {
var sb strings.Builder
b := make([]byte, 0, 10)
for i, s := range a {
b = strconv.AppendInt(b[:0], int64(i), 10)
b = append(b, " === "...)
b = append(b, s...)
b = append(b, '\n')
sb.WriteString(string(b))
}
return sb.String()
}
func buildString3(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(fmt.Sprintf("%d === %s\n", i, s))
}
return sb.String()
}
func buildString2(strSlice []string) string {
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf("%d === %s\n", index, element)
}
return bigString
}
func Benchmark1(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString(strSlice)
}
}
func Benchmark2(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString2(strSlice)
}
}
func Benchmark3(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString3(strSlice)
}
}
func Benchmark4(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString4(strSlice)
}
}
func Benchmark5(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString5(strSlice)
}
}
func Benchmark6(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString6(strSlice)
}
}
Go 版本为 go1.17.1。
英文:
You may use strings.Builder
, which is more efficient, try this:
func buildString(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteRune('\n')
}
return sb.String()
}
Benchmark:
Benchmark1-8 4203799 252 ns/op 56 B/op 3 allocs/op
Benchmark6-8 5044305 280 ns/op 56 B/op 3 allocs/op
Benchmark4-8 4332459 319 ns/op 72 B/op 3 allocs/op
Benchmark5-8 2792514 371 ns/op 56 B/op 3 allocs/op
Benchmark3-8 1000000 1018 ns/op 144 B/op 8 allocs/op
Benchmark2-8 1000000 1109 ns/op 152 B/op 8 allocs/op
Code:
package main
import (
"fmt"
"strconv"
"strings"
"testing"
)
func buildString(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteRune('\n')
}
return sb.String()
}
func buildString6(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === ")
sb.WriteString(s)
sb.WriteString("\n")
}
return sb.String()
}
func buildString5(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(" === " + s + "\n")
}
return sb.String()
}
func buildString4(a []string) string {
var sb strings.Builder
b := make([]byte, 0, 10)
for i, s := range a {
b = strconv.AppendInt(b[:0], int64(i), 10)
b = append(b, " === "...)
b = append(b, s...)
b = append(b, '\n')
sb.WriteString(string(b))
}
return sb.String()
}
func buildString3(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(fmt.Sprintf("%d === %s\n", i, s))
}
return sb.String()
}
func buildString2(strSlice []string) string {
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf("%d === %s\n", index, element)
}
return bigString
}
func Benchmark1(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString(strSlice)
}
}
func Benchmark2(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString2(strSlice)
}
}
func Benchmark3(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString3(strSlice)
}
}
func Benchmark4(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString4(strSlice)
}
}
func Benchmark5(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString5(strSlice)
}
}
func Benchmark6(b *testing.B) {
strSlice := []string{"one", "two", "three"}
for i := 0; i < b.N; i++ {
_ = buildString6(strSlice)
}
}
go version go1.17.1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论