如何用多维数组(最好使用goroutine或channel)创建字符串,以便用句号替换元素的最后一个逗号?
谢谢
package main
import (
"fmt"
)
func main() {
pls := [][]string {
{"C", "C++"},
{"JavaScript"},
{"Go", "Rust"},
}
for _, v1 := range pls {
for _, v2 := range v1 {
fmt.Print(v2,", ")
}
}
}
最佳答案
我猜经典的strings.Join
会更易于实现和维护:
package main
import (
"fmt"
"strings"
)
func main() {
pls := [][]string{
{"C", "C++"},
{"JavaScript"},
{"Go", "Rust"},
}
var strs []string
for _, v1 := range pls {
s := strings.Join(v1, ", ")
strs = append(strs, s)
}
s := strings.Join(strs, ", ")
fmt.Println(s)
}
https://play.golang.org/p/2Nuv00PV5j
关于go - 需要将2维数组转换为字符串,并用句号替换最后一个逗号。(Golang),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46624446/