Last active
October 15, 2021 07:13
-
-
Save tenox7/5cbc372835ebf766981a5b9aee73a25e to your computer and use it in GitHub Desktop.
HTML Select Option Generator in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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