按数组值切片

时间:2019-03-25 09:34:30

标签: json api go beego

如何按ID切片数组

{
  "id": 1
  "status": "available" 
  "snapshot_id" : 1
  ...
},
{
   "id": 2
   "status" "non available"
   "snapshot_id" : 0
   ...
}

我没有从数据库中获取任何东西。需要像这样按值划分,但不是来自数据库

o.QueryTable("Ticket").Filter("Status", "Pending").All(&newticket)

1 个答案:

答案 0 :(得分:3)

根据idstatussnapshot_id等值参数从现有数组创建子数组

我已经重现了您的场景并根据值参数对数组进行了切片

package main

import (
    "fmt"
)

type Ticket struct {
    id          int
    status      string
    snapshot_id int
}

func main() {

    var newTicket []Ticket

    TicketArr := []Ticket{{1, "available", 1}, {2, "n.a", 2}, {3, "available", 30}}

    for _, v := range TicketArr {

        if v.status == "available" {

            newTicket = append(newTicket, v)
        }
    }

    fmt.Println("newTicket", newTicket)

}

输出:

newTicket [{1 available 1} {3 available 30}]