In GoLang I am looping through my form data like this,
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
I can print the data out to the terminal like this but I need this data to be a string so I can access it from outside of the for loop.
How can I Put this in a variable that I can call outside of the for loop when the loop is finished?
My goal here is to build a url by joining these three strings
var SearchUrl = "https://api.themoviedb.org/3/search/movie?query="
var MovieSearch []string = r.Form["GetSearchKey"]
var apiKey = "&api_key=e2a"
UrlBuild := []string {SearchUrl, MovieSearch, apiKey}
fmt.Println(UrlBuild)
I get this error
/main.go:71: cannot use MovieSearch (type []string)
as type string in array or slice literal
MovieSearch
is coming from the input form. It is the search keywords.
Once I have one string I can pass this to a function that makes the api call.
The full function
func searchHandler(w http.ResponseWriter, r *http.Request) {
display(w, "search", &Page{Title: "Search"})
fmt.Println("method:", r.Method)
r.ParseForm()
fmt.Println("GetSearchKey:", r.Form["GetSearchKey"])
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
var SearchUrl = "https://api.themoviedb.org/3/search/movie?query="
var MovieSearch []string = r.Form["GetSearchKey"]
var apiKey = "&api_key=ewrfwrfwrcwerc"
UrlBuild := []string {SearchUrl, MovieSearch, apiKey}
fmt.Println(UrlBuild)
fmt.Println(reflect.TypeOf(r.Form["GetSearchKey"] ))
}
The error comes from this line,
UrlBuild := []string {SearchUrl, MovieSearch, apiKey}