【问题标题】:how do i append a string to a slice inside a slice? [closed]如何将字符串附加到切片内的切片? [关闭]
【发布时间】:2020-12-31 19:44:05
【问题描述】:
 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的“name”,我怎样才能附加其余的变量呢?

【问题讨论】:

  • 你的问题一点都不清楚。您究竟希望输出是什么?

标签: arrays loops go slice


【解决方案1】:

你想这样做吗?

package main

import (
    "fmt"
)

func main() {

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

    ss := [][]string{ []string{"name", "place", "thing"}, []string{"name", "place", "thing"}, []string{"name", "place", "thing"} }


    for i := range ss {
        for j := range ss[i] {
            sos[i] = append(sos[i], ss[j][i])
        }
    }

    fmt.Println(sos)
}

输出:

[[姓名姓名][地点地点][事物事物]]

Go playGround

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2015-11-09
    • 2020-11-27
    • 2017-08-11
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多