Created
April 8, 2013 08:35
-
-
Save jgraham909/5335201 to your computer and use it in GitHub Desktop.
golang template idea
This file contains hidden or 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
package main | |
import "fmt" | |
import "bytes" | |
import "html" | |
func main() { | |
s := Widget("myWidget", "checkbox", map[string] string{"checked": "checked", "someattr": "Value"}) | |
fmt.Println(s) | |
} | |
func Widget(n,t string, attr map[string]string) string { | |
var b bytes.Buffer | |
b.WriteString(fmt.Sprintf(`<input name="%s" type="%s" `, n, t)) | |
for k, v := range attr { | |
b.WriteString(fmt.Sprintf(` %s="%s"`, k, html.EscapeString(v))) | |
} | |
b.WriteString(" />") | |
return b.String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment