How to iterate over a slice and build one string from the output\

huangapple go评论91阅读模式
英文:

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(&quot; === &quot;)
sb.WriteString(s)
sb.WriteRune(&#39;\n&#39;)
}
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 (
&quot;fmt&quot;
&quot;strconv&quot;
&quot;strings&quot;
&quot;testing&quot;
)
func buildString(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(&quot; === &quot;)
sb.WriteString(s)
sb.WriteRune(&#39;\n&#39;)
}
return sb.String()
}
func buildString6(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(&quot; === &quot;)
sb.WriteString(s)
sb.WriteString(&quot;\n&quot;)
}
return sb.String()
}
func buildString5(a []string) string {
var sb strings.Builder
for i, s := range a {
sb.WriteString(strconv.Itoa(i))
sb.WriteString(&quot; === &quot; + s + &quot;\n&quot;)
}
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, &quot; === &quot;...)
b = append(b, s...)
b = append(b, &#39;\n&#39;)
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(&quot;%d === %s\n&quot;, i, s))
}
return sb.String()
}
func buildString2(strSlice []string) string {
var bigString string
for index, element := range strSlice {
bigString += fmt.Sprintf(&quot;%d === %s\n&quot;, index, element)
}
return bigString
}
func Benchmark1(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString(strSlice)
}
}
func Benchmark2(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString2(strSlice)
}
}
func Benchmark3(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString3(strSlice)
}
}
func Benchmark4(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString4(strSlice)
}
}
func Benchmark5(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString5(strSlice)
}
}
func Benchmark6(b *testing.B) {
strSlice := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
for i := 0; i &lt; b.N; i++ {
_ = buildString6(strSlice)
}
}

go version go1.17.1

huangapple
  • 本文由 发表于 2021年10月1日 21:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/69406465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定