我需要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 是否有內置函數來做上面的事情
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消