我正在嘗試創建切片。在所有示例中,內部切片都是基于整數的。我正在嘗試創建一個字符串切片。例子:[[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)
}
- 1 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消