1

I am trying to create an extensive search url with this format ?filter[1][field]=brandId&filter[1][operand]=>&filter[1][values][]=firstvalue but I cant seem to figure out a way to convert this (string) to an actual array (map/slice or anything i can loop on).

i have read a lot of documentation and searched on google but cannot find a good way to do this.

Rohit Hazra
  • 660
  • 9
  • 25

1 Answers1

0

You could range on the parsed query like so if your filter is going to be an unknown length:

https://play.golang.org/p/NSQ7bnJXef

    v, err := url.ParseQuery("filter[1][field]=brandid&filter[1][operand]=>&filter[1][values][]=firstvalue&filter[2][field]=brandid&filter[2][operand]=>&filter[2][values][]=firstvalue")
    if err != nil {
        fmt.Println(err)
        return
    }
    for id, thing := range v {
        switch {
        case strings.Contains(id, "field"):
            log.Printf("Field Value: %v", thing)
        case strings.Contains(id, "values"):
            log.Printf("Values Value: %v", thing)
        case strings.Contains(id, "operand"):
            log.Printf("Operand Value: %v", thing)
        }
    }

But IMO, there has to be a better way than doing the query you're doing now.

Datsik
  • 13,003
  • 13
  • 70
  • 108