Created
June 7, 2012 17:34
-
-
Save prasmussen/2890229 to your computer and use it in GitHub Desktop.
go-template-example
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
package main | |
import ( | |
"fmt" | |
"bytes" | |
"text/template" | |
) | |
type Person struct { | |
Name string | |
} | |
func render(s string, i interface{}) string { | |
t, err := template.New("person").Parse(s) | |
if err != nil { | |
fmt.Println(err) | |
return "" | |
} | |
var buf bytes.Buffer | |
t.Execute(&buf, i) | |
return buf.String() | |
} | |
func main() { | |
s := render("Hello {{.Name}}!", &Person{Name: "Petter"}) | |
fmt.Println(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment