Re: [go-nuts] how to check if url.Values contain some attributes or not ?

3,112 views
Skip to first unread message

Scott Lawrence

unread,
Mar 28, 2013, 1:48:44 PM3/28/13
to suhas kodgire, golan...@googlegroups.com
On Thu, 28 Mar 2013, suhas kodgire wrote:

>
> I have written a web service in which I send parameters as query string .
> In the program I convert those parameters to url.Values by Query method .
>
> I get all the parameters there when I print them on console , I get
> following result
>
> map[page_number:[1] per_page:[24] filters[min_price]:[0] filters[dogs]:[0]
> filters[cats]:[0]]
>
>
> Now I need to check if parameter contains any fiilters .
>
> One way to do it check for params["filters[min_price]"] ,
> params["filters[dog]"] , params["filters[cat]"] etc with empty (nil)
> condition .
> Is there any way to check this effectively ?

The actual type of url.Values is "map[string][]string". So you can loop over
the keys,

for key, val := range myUrl.Query() {
if strings.IsPrefix("filters", key) {
doFilter(key, val)
}
}

Or something along those lines.

>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

--
Scott Lawrence

go version go1.0.3
Linux baidar 3.8.3-2-ARCH #1 SMP PREEMPT Sun Mar 17 13:04:22 CET 2013 x86_64 GNU/Linux

Kyle Lemons

unread,
Mar 28, 2013, 10:35:55 PM3/28/13
to suhas kodgire, golang-nuts
Are you looking for something like

if _, ok := r.Form[key]; ok {
  // key exists as a param
}


On Thu, Mar 28, 2013 at 8:27 AM, suhas kodgire <kodgir...@gmail.com> wrote:

I have written a web service in which I send parameters as query string . In the program I convert those parameters to url.Values by Query method .

I get all the parameters there when I print them on console , I get following result

map[page_number:[1]  per_page:[24] filters[min_price]:[0] filters[dogs]:[0] filters[cats]:[0]]


Now I  need to check if parameter contains any fiilters .

One way to do it check  for params["filters[min_price]"] , params["filters[dog]"] , params["filters[cat]"] etc with empty (nil) condition .
Is there any way to check this effectively  ?

--

suhas kodgire

unread,
Mar 29, 2013, 1:40:55 AM3/29/13
to golan...@googlegroups.com
Thanks for the quick reply , I was looking for he similar thing .
I used HasPrefix to achieve that

   for key, val := range title {
      if strings.HasPrefix(key,"filters") {
          fmt.Println(key,":",val)
Reply all
Reply to author
Forward
0 new messages