How to fill a select html with map interface

OK I see it’s the syntax that is problematic tell me if I’m in the right

package main
 import (
"net/http"
"html/template"
)
 var fruits = map[string]interface{}{
     "Apple":  "apple",
     "Orange": "orange",
     "Pear":   "pear",
     "Grape":  "grape",
 }

 func handler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("view.html") //setp 1
t.Execute(w, fruits) //step 2
}

func main() {
server := http.Server{
    Addr: "127.0.0.1:8080",
}
http.HandleFunc("/view", handler)
server.ListenAndServe()
}
 /htmlcode for view.html
<html>
<head>
  <title>First Program</title>
  </head>
  <body>
  <select>{{range $ key, $ value: =. Fruits}} <option>{{$ key}} </option></select>
  {{end}}
  </body>

does it seem to you that I do not take it well?

1 Like