func index(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
My question is how do I r.Form this to another function? Like this
Thank you! It seems to be on the right track…, but
func Send(form url.Values) {
ParseForm(form)
for key, values := range form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
The error: “undefined: ParseForm”. How can I use ParseForm on the form variable?
You don’t parse the form in the Send function, your parse it in your handler so that the Form field gets populated and the you pass that Form (which is of type url.Values) to your Send function. So something like this:
func index(w http.ResponseWriter, r *http.Request) {
// The r.Form field is only available after ParseForm is called so you have to parse first.
r.ParseForm()
//Do some form checking or whatever work you need here, then call Send with the values.
err := mail.Send(r.Form)
if err != nil {
//Handle your error.
}
//Continue with whatever work you need done.
}