dhaval agrawal
dhaval agrawal

Reputation: 33

How to iterate over query params in Golang

I am using Fiber package in Golang to handle the httprequests and want to find all the query params present in request

GET http://example.com/shoes?order=desc&brand=nike&lang=english

I want to iterate over query params and looking for something like this

for k, v := range c.AllQueries() {
    reqObj[k] = v[0]
}

Expected output:

"order" : "desc"
"brand" : "nike"
"lang" : "english"

Upvotes: 0

Views: 2020

Answers (1)

Dipto Mondal
Dipto Mondal

Reputation: 764

Here is the link of AllParams implementation. We can see this method returns a map[string]string of query params. So AllParams is what you need.

params := c.AllParams()
for k, v := range params {
    // do something with the values
}

Edit: Go-fiber uses fasthttp under the hood. You can look into this method block and iterate the fasthttp.QueryArgs() the way they are doing.

c.fasthttp.QueryArgs().VisitAll(func(key, val []byte) {
   // implement what you want to do with the key values here
})

Upvotes: 3

Related Questions