第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在gin中獲取參數(shù)值數(shù)組

如何在gin中獲取參數(shù)值數(shù)組

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,    }}我的結(jié)構(gòu):type Params struct {    Limit  int   `json:"limit"`    Page   int   `json:"page"`    Cat_id []int `json:"cat_id"`}在我的函數(shù)中請求:result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id=?", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)我在網(wǎng)上看了很多文章,但沒有一條建議對我有幫助。
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗 獲得超4個贊

queryValue是一個字符串,您可以對其應(yīng)用您認(rèn)為合適的任何操作:


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 是否有內(nèi)置函數(shù)來做上面的事情


查看完整回答
反對 回復(fù) 2023-01-03
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號