Skip to content

Instantly share code, notes, and snippets.

@smiley-yoyo
Last active July 3, 2024 06:17
Show Gist options
  • Select an option

  • Save smiley-yoyo/72758c2c2fd461bd5357ebb9764366b9 to your computer and use it in GitHub Desktop.

Select an option

Save smiley-yoyo/72758c2c2fd461bd5357ebb9764366b9 to your computer and use it in GitHub Desktop.
parse query to map[string]any with gin
import (
"github.com/gin-gonic/gin"
)
type QueryMap map[string]any
func ParseQuery(c *gin.Context) (QueryMap, error) {
query := make(QueryMap)
if c.Request.Method == "GET" {
for k, v := range c.Request.URL.Query() {
if len(v) == 1 {
query[k] = v[0]
} else {
query[k] = v
}
}
} else {
// also try to parse from body
if err := c.Bind(&query); err != nil {
return nil, err
}
}
return query, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment