i am trying to write http api on GoLang. When i am comparing 2 string, i am taking this error "invalid operation: a.TypeI == m["type"][0] (mismatched types []string and string)". How can i fix, can anyone help ?
func listHandler(w http.ResponseWriter, r *http.Request) {
u, errUrl := url.Parse(r.URL.String())
check(errUrl)
m, _ := url.ParseQuery(u.RawQuery)
dat, err := ioutil.ReadFile("data.json")
check(err)
var basedata BaseData
err2 := json.Unmarshal(dat, &basedata)
check(err2)
for _, a := range basedata.Pokemons {
if a.TypeI == m["type"][0] || a.TypeII == m["type"][0] {
fmt.Fprintln(w, "dosomething")
}
}
}
Looking at the Go docs for the url
package, you want Values.Get
:
if a.TypeI == m.Get("type") || a.TypeII == m.Get("type") {
// do something
}
Furthermore, can you print the value of r.URL.String()
for me like this:
fmt.Println(r.URL.String())