Golang : Populate dropdown with html/template example
Creating a clickable dropdown button or HTML select tag is a common task for populating a form. Here is a simple example how to create HTML clickable dropdown or select tag in Golang:
package main
import (
"net/http"
)
func SimpleSelectTag(w http.ResponseWriter, r *http.Request) {
html := `<!DOCTYPE html>
<html>
<body>
<select>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="pear">Pear</option>
<option value="grape">Grape</option>
</select>
</body>
</html>`
w.Write([]byte(html))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", SimpleSelectTag)
http.ListenAndServe(":8080", mux)
}
Run the code above and point your browser to localhost:8080
or yourwebsitename.com:8080
should give you a dropdown button.
HTML output:
<!DOCTYPE html>
<html>
<body>
<select>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="pear">Pear</option>
<option value="grape">Grape</option>
</select>
</body>
</html>
now, if you are extracting the dropdown elements from a database, you need to loop through the elements in order to populate the dropdown. This next example will show you how to do just that with html/template
package and how to do a for loop
inside the template.
package main
import (
"html/template"
"net/http"
)
type DropdownItem struct {
Name string
Value string
}
var fruits = map[string]interface{}{
"Apple": "apple",
"Orange": "orange",
"Pear": "pear",
"Grape": "grape",
}
var capitals = map[string]interface{}{
"Japan": "Tokyo",
"Australia": "Canberra",
"New Zealand": "Auckland",
"China": "Beijing",
"Russia": "Moscow",
"Malaysia": "Kuala Lumpur",
}
func Dropdown(w http.ResponseWriter, r *http.Request) {
html := `<!DOCTYPE html>
<html>
<body>
<select> // for loop in html template example
{{range $key, $value := .}}
<option value="{{ $value }}">{{ $key }}</option>
{{end}}
</select>
</body>
</html>`
dropdownTemplate, err := template.New("dropdownexample").Parse(string(html))
if err != nil {
panic(err)
}
// populate dropdown with fruits
dropdownTemplate.Execute(w, fruits)
// populate dropdown with capital cities
dropdownTemplate.Execute(w, capitals)
// no need for this...
//w.Write([]byte(html))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", Dropdown)
http.ListenAndServe(":8080", mux)
}
Run the code above and refresh you browser. You should be able to see the following output in the page source code.
<!DOCTYPE html>
<html>
<body>
<select>
<option value="apple">Apple</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="pear">Pear</option>
</select>
</body>
</html><!DOCTYPE html>
<html>
<body>
<select>
<option value="Canberra">Australia</option>
<option value="Beijing">China</option>
<option value="Tokyo">Japan</option>
<option value="Kuala Lumpur">Malaysia</option>
<option value="Auckland">New Zealand</option>
<option value="Moscow">Russia</option>
</select>
</body>
</html>
Hope this helps! To process the values from the dropdown button, see how to use FormValue
in https://www.socketloop.com/tutorials/golang-get-checkbox-or-extract-multipart-form-data-value-example
Check Out As Well:
References:
https://www.socketloop.com/references/golang-html-template-template-parse-function-example
http://stackoverflow.com/questions/21302520/golang-iterating-through-map-in-template
See also : Golang : Get checkbox or extract multipart form data value example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.2k Golang : Get S3 or CloudFront object or file information
+5.9k Golang : How to write backslash in string?
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+13.9k Golang : Simple word wrap or line breaking example
+39.8k Golang : Convert to io.ReadSeeker type
+7.3k Golang : Dealing with struct's private part
+5.1k Responsive Google Adsense
+5.3k Clean up Visual Studio For Mac installation failed disk full problem
+14.7k Golang : Search folders for file recursively with wildcard support
+10.8k Golang : Read until certain character to break for loop
+5.6k Golang : Markov chains to predict probability of next state example
+13.3k Golang : reCAPTCHA example