I have some code that pulls out the URL parameters from a URL and places it into a map. I can successfully print out the values of the map items that I want to examine.
values := r.URL.Query()
a := values["a"]
fmt.Println(a)
I notice when the values print they print with "[]" surrounding them.
I'd like to value the value of each a,b,c and check to see if it is contained in a comma-delimited string.
allowedA = "value1,value2,value3"
i.e. something similar to:
if(contains(allowedA,a)
Meaning "if the value in 'a' is contained in the variable 'allowedA' then return true.
Any suggestions?
I notice when the values print they print with "[]" surrounding them.
That's because url.Values
is map[string][]string
, not map[string]string
. Use url.Values.Get
to access the parameter directly.
I'd like to value the value of each a,b,c and check to see if it is contained in a comma-delimited string.
I'd suggest splitting the string and using the result as map keys, so that you could use the map as a set. I.e.
allowedAMap := map[string]struct{}{}
allowedAStrs := strings.Split(allowedA, ",")
for _, s := range allowedAStrs {
allowedAMap[s] = struct{}{}
}
//...
if _, ok := allowedAMap[a]; !ok {
fmt.Println("wrong a")
return
}
fmt.Println("correct a")