如何将字符串附加到切片内的切片?

sos := make([][]string,3)


ss := [["name","place","thing"],["name","place","thing"],["name","place","thing"]]

让 sos 成为 [["name","name"],["place","place"],["thing","thing"] 我如何附加名称、地点和事物字段?最好使用从 ss 到 sos 的 for 循环?


 for i:=0;i<len(sos);i++ {

    sos[i] = append(sos[i],ss[0])

    }

上面的 for 循环只能附加第一个变量,即从 ss 到 sos 的“名称”,我怎样才能附加其余的变量呢?


红颜莎娜
浏览 102回答 1
1回答

Cats萌萌

你想这样做吗?package mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; sos := make([][]string, 3)&nbsp; &nbsp; ss := [][]string{ []string{"name", "place", "thing"}, []string{"name", "place", "thing"}, []string{"name", "place", "thing"} }&nbsp; &nbsp; for i := range ss {&nbsp; &nbsp; &nbsp; &nbsp; for j := range ss[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sos[i] = append(sos[i], ss[j][i])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(sos)}输出:[[姓名姓名][地方地方][东西东西]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go