切片,groupBy []数组

时间:2019-04-01 03:45:04

标签: api go beego

作为[]数组,我遇到了所有服务器问题。问题是如何切片(groupBy)状态,可启动。我只想将状态=可用,可启动=假

控制器

slicedBy := make(map[string]interface{})

server := blockstorage.ListVolumes(tenantID.(string))   <----- this is array

for _, sg := range server{
    slicedBy[sg.ID] = sg.Status                    <------- slice by Status
    slicedBy[sg.ID] = sg.Bootable                <------- slice by Bootable
}

Json阵列

{
   id        123
   status    available
   bootable  false
   ...
}

服务器作为阵列

[
    {
        "id": "a8b123fc-a141-4682-b65b-d56899621959",
        "status": "available",
        "size": 1,
        "availability_zone": "nova",
        "attachments": [],
        "name": "snapshot 1",
        "description": "",
        "bootable": "false",
    },
    {
        "id": "ccb734d4-c098-4929-8ce5-281b6a58421d",
        "status": "error",
        "size": 2,
        "availability_zone": "nova",
        "attachments": [],
        "name": "",
        "description": "",
        "volume_type": "",
        "bootable": "false",
        "volume_image_metadata": {
            "signature_verified": "False"
        }
    },

1 个答案:

答案 0 :(得分:1)

for _, sg := range server {
  if(sg.Status == "available" && sg.Bootable == "false"){
     jsons["Data"] = sg
  }
}

效果很好。我刚刚找到了。