如何按ID切片数组
{
"id": 1
"status": "available"
"snapshot_id" : 1
...
},
{
"id": 2
"status" "non available"
"snapshot_id" : 0
...
}
我没有从数据库中获取任何东西。需要像这样按值划分,但不是来自数据库
o.QueryTable("Ticket").Filter("Status", "Pending").All(&newticket)
答案 0 :(得分:3)
根据id
、status
和snapshot_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}]