为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
蛊毒传说 2022-10-04 17:14:40
如何循环访问切片并从输出中生成新的单个字符串,然后在函数中返回该字符串?例如,如果我想读取切片并为每个项目添加索引,则将新更改放在单个字符串中并在函数中返回: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}
查看完整描述

2 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

我完全同意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)

}

但是,请参加马克推荐给你的围棋之旅。


查看完整回答
反对 回复 2022-10-04
?
杨魅力

TA贡献1811条经验 获得超6个赞

您可以使用,这更有效,请尝试以下操作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




查看完整回答
反对 回复 2022-10-04
  • 2 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信