gpt4 book ai didi

amazon-web-services - golang附加到二维 slice

转载 作者:行者123 更新时间:2023-12-01 22:05:47 24 4
gpt4 key购买 nike

我正在尝试创建 slice slice 。

在所有示例中,内部 slice 都是基于整数的。

我正在尝试创建一个字符串 slice 的 slice 。

例子:

[
[Name1,State1,Tags.Owner1]
[Name2,State2,Tags.Owner2]
[Name3,State3,Tags.Owner3]
]

我正在尝试这样做:
outerList :=  [][]string{}

i := 0
for _,c := range clusters {
input := &eks.DescribeClusterInput{
Name: aws.String(c),
}

resp,err := svc.DescribeCluster(input)
if err != nil {
errorOut(`clusterData function: `+err.Error())
}
record := resp.Cluster
data,_ := json.Marshal(record)
error := json.Unmarshal(data, &cluster)
if error != nil {errorOut(error.Error())}
innerList := [...]string{cluster.Name,cluster.Tags["Vsad"],cluster.Status}
outerList[string(i)] = innerList
}

我收到以下错误:
non-integer slice index string(i) 
cannot use innerList (type [3]string) as type []string in assignment

我知道在Python中我可以做:
outerList = list()

for c in cluster:
a = [c.Name,c.State,c.Tags.Owner]
outerList.append(a)

最佳答案

您可以使用append。格式如下:

// make room for clusters
outerList := make([][]string, len(clusters))

// iterate and fill cluster data
for i, c := range clusters {
// some processing where cluster variable is setupped

// add new inner slice
outerList[i] = append(outerList[i], cluster.Name, cluster.Tags["Vsad"], cluster.Status)
}

关于amazon-web-services - golang附加到二维 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62012614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com