如何循环访问切片并从输出生成一个字符串\

如何循环访问切片并从输出中生成新的单个字符串,然后在函数中返回该字符串?


例如,如果我想读取切片并为每个项目添加索引,则将新更改放在单个字符串中并在函数中返回:


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

}


蛊毒传说
浏览 116回答 2
2回答

阿晨1998

我完全同意Marc的评论,但为了完整起见,以下是您尝试完成的可能解决方案: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)}但是,请参加马克推荐给你的围棋之旅。

杨魅力

您可以使用,这更有效,请尝试以下操作:strings.Builderfunc buildString(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(i))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(" === ")&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(s)&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune('\n')&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}基准:Benchmark1-8&nbsp; 4203799&nbsp; &nbsp;252 ns/op&nbsp; &nbsp;56 B/op&nbsp; 3 allocs/opBenchmark6-8&nbsp; 5044305&nbsp; &nbsp;280 ns/op&nbsp; &nbsp;56 B/op&nbsp; 3 allocs/opBenchmark4-8&nbsp; 4332459&nbsp; &nbsp;319 ns/op&nbsp; &nbsp;72 B/op&nbsp; 3 allocs/opBenchmark5-8&nbsp; 2792514&nbsp; &nbsp;371 ns/op&nbsp; &nbsp;56 B/op&nbsp; 3 allocs/opBenchmark3-8&nbsp; 1000000&nbsp; 1018 ns/op&nbsp; 144 B/op&nbsp; 8 allocs/opBenchmark2-8&nbsp; 1000000&nbsp; 1109 ns/op&nbsp; 152 B/op&nbsp; 8 allocs/op法典:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "testing")func buildString(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(i))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(" === ")&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(s)&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune('\n')&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func buildString6(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(i))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(" === ")&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(s)&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString("\n")&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func buildString5(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(i))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(" === " + s + "\n")&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func buildString4(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; b := make([]byte, 0, 10)&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; b = strconv.AppendInt(b[:0], int64(i), 10)&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, " === "...)&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, s...)&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, '\n')&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(string(b))&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func buildString3(a []string) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; for i, s := range a {&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(fmt.Sprintf("%d === %s\n", i, s))&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func buildString2(strSlice []string) string {&nbsp; &nbsp; var bigString string&nbsp; &nbsp; for index, element := range strSlice {&nbsp; &nbsp; &nbsp; &nbsp; bigString += fmt.Sprintf("%d === %s\n", index, element)&nbsp; &nbsp; }&nbsp; &nbsp; return bigString}func Benchmark1(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString(strSlice)&nbsp; &nbsp; }}func Benchmark2(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString2(strSlice)&nbsp; &nbsp; }}func Benchmark3(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString3(strSlice)&nbsp; &nbsp; }}func Benchmark4(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString4(strSlice)&nbsp; &nbsp; }}func Benchmark5(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString5(strSlice)&nbsp; &nbsp; }}func Benchmark6(b *testing.B) {&nbsp; &nbsp; strSlice := []string{"one", "two", "three"}&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _ = buildString6(strSlice)&nbsp; &nbsp; }}go 版本 go1.17.1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go