Skip to content

Instantly share code, notes, and snippets.

@tenox7
Last active October 15, 2021 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenox7/5cbc372835ebf766981a5b9aee73a25e to your computer and use it in GitHub Desktop.
Save tenox7/5cbc372835ebf766981a5b9aee73a25e to your computer and use it in GitHub Desktop.
HTML Select Option Generator in Go
// easily create html form select/option with default selection (selected) and disabled
package main
import (
"fmt"
"strings"
)
func selOpt(s string, f ...struct{ v, n string }) string {
var o []string
var m = make(map[string]string)
m[s] = "selected"
m[""] = "disabled"
for _, i := range f {
o = append(o, fmt.Sprintf("<option value=\"%v\" %v>%v</option>", i.v, m[i.v], i.n))
}
return strings.Join(o, "\n")
}
func main() {
fmt.Printf("<select>%v</select>",
selOpt("y", []struct{ v, n string }{
{"y", "Yes"},
{"n", "No"},
{"", "-----"},
{"m", "Maybe"},
}...))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment