亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang 附加到 2d 切片

golang 附加到 2d 切片

Go
MMMHUHU 2022-06-06 15:46:53
我正在嘗試創建切片。在所有示例中,內部切片都是基于整數的。我正在嘗試創建一個字符串切片。例子:[[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)
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您可以使用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)

}


查看完整回答
反對 回復 2022-06-06
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號