这可能是过度设计的,但它为我完成了一些事情:
// @Param property_type query []uint32 false "property_types"
func (svc *HttpService) acceptListofQuery(c *gin.Context) {
var propertyTypes []uint32
propertyTypes, done := svc.parsePropTypes(c)
if done {
return
}
// ..... rest of logic here
c.JSON(200, "We good")
}
func (svc *HttpService) parsePropTypes(c *gin.Context) ([]uint32, bool) {
var propertyTypes []uint32
var propertyTypesAsString string
var propertyTypesAsArrayOfStrings []string
propertyTypesAsString, success := c.GetQuery("property_types")
propertyTypesAsArrayOfStrings = strings.Split(propertyTypesAsString, ",")
if success {
for _, propertyTypeAsString := range propertyTypesAsArrayOfStrings {
i, err := strconv.Atoi(propertyTypeAsString)
if err != nil {
svc.ErrorWithJson(c, 400, errors.New("invalid property_types array"))
return nil, true
}
propertyTypes = append(propertyTypes, uint32(i))
}
}
return propertyTypes, false
}
然后你可以发送类似的东西:
curl -X POST 'http://localhost:4000/url?property_types=1,4,7,12