亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在gin中獲取參數值數組

如何在gin中獲取參數值數組

Go
POPMUISE 2023-01-03 17:15:09
我需要cat_id在請求中接受多個值,例如:localhost:8080/products/?cat_id=1,2我的功能:func GenerateMultiParams(c *gin.Context) models.Params {    limit := 0    page := 1    cat_id := 1    query := c.Request.URL.Query()    for key, value := range query {        queryValue := value[len(value)-1]        switch key {        case "limit":            limit, _ = strconv.Atoi(queryValue)        case "page":            page, _ = strconv.Atoi(queryValue)        case "cat_id":            cat_id, _ = strconv.Atoi(queryValue)        }    }    return models.Params{        Limit:  limit,        Page:   page,        Cat_id: []cat_id,    }}我的結構:type Params struct {    Limit  int   `json:"limit"`    Page   int   `json:"page"`    Cat_id []int `json:"cat_id"`}在我的函數中請求:result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id=?", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)我在網上看了很多文章,但沒有一條建議對我有幫助。
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

queryValue是一個字符串,您可以對其應用您認為合適的任何操作:


func toIntArray(str string) []int {

    chunks := strings.Split(str, ",")


    var res []int

    for _, c := range chunks {

        i, _ := strconv.Atoi(c) // error handling ommitted for concision

        res = append(res, i)

    }


    return res

}

我不知道 Gin 是否有內置函數來做上面的事情


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號