Params模型中,我有一个int Cat_id数组

我请求:localhost:8080/products/?cat_id=1,2

func GetAllIproducts(q *models.Products, pagination *models.Params) (*[]models.Products, error) {
    var prod []models.Products
    offset := (pagination.Page - 1) * pagination.Limit
    result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id=?", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod) //Problem is here
    if result.Error != nil {
        msg := result.Error
        return nil, msg
    }
    return &prod, nil
}

当我使用Debug时,我得到了这个:

推荐答案

假设cat_id是一个整数(假设为int64),您可以做以下两件事:

  1. pagination.Cat_id个字符串转换为[]int64个片段(我们将此变量称为[]int64类型的catIDs),以获得具有分离的int64个元素的片段.

  2. 将您的Where子句更改为以下内容:

     result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", catIDs).Limit(pagination.Limit).Offset(offset).Find(&prod)
    

Go相关问答推荐

出口上下文值密钥的安全方法?

Golang应用程序:所请求的资源上不存在HTTP-Control-Allow-Origin标头

即使HTTP服务器正在使用GO和Protobuf、SQL Server启动,请求也不返回结果

租户GUID X的租户不存在self 邮箱帐户的租户(我是唯一的成员)

创建服务时云运行触发器执行失败

我可以在Golang中的另一个类型参数的基础上约束一个类型的参数吗?

死锁 - 所有 goroutine 都处于睡眠状态(即使使用等待组)

Json.Unmarshal() 和 gin.BindJson() 之间的区别

GORM中是否可能自动迁移具有循环关系的表?

带有前导零的整数参数被 flag.IntVar 解析为八进制

同一文件上的多个 Arrow CSV 阅读器返回 null

如何在 Golang 中打印 2 列表?

如何将 base64 编码的公钥转换为 crypto.PublicKey 或 ecdsa.PublicKey

使用 Go 解组 SOAP 消息

Apache Beam 左加入 Go

使用 Grafana alert 在几分钟内重复alert

为什么当我忽略 template.New() 程序可以成功运行?

如何在 golang 中同时加载 .env 文件和 os 环境变量

为什么 0 big.Int 的 .Bytes() 值是空切片?

Golang 查询扫描未将查询正确扫描到 struct 中