Created
January 12, 2016 14:40
-
-
Save asemt/54b6ff0024b9d1fe8b2a to your computer and use it in GitHub Desktop.
Stupid simple Go template example
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
/* | |
Go text template example. | |
References: | |
- https://golang.org/pkg/text/template/ | |
- http://stackoverflow.com/questions/13765797/the-best-way-to-get-a-string-from-a-writer-in-go | |
- http://andlabs.lostsig.com/blog/2014/05/26/8/the-go-templates-post | |
- https://jan.newmarch.name/go/template/chapter-template.html | |
- http://nathanleclaire.com/blog/2014/07/19/demystifying-golangs-io-dot-reader-and-io-dot-writer-interfaces/ | |
*/ | |
package main | |
import( | |
"fmt" | |
"text/template" | |
"bytes" | |
) | |
type Credentials struct { | |
Key, Secret string | |
} | |
func main(){ | |
creds := Credentials{"key", "secret"} | |
tmpl, err := template.New("creds").Parse("{{.Key}}:{{.Secret}}") | |
if err != nil { panic(err) } | |
res := new(bytes.Buffer) | |
err = tmpl.Execute(res, creds) | |
if err != nil { panic(err) } | |
fmt.Printf("Rendered Template: '%s'", res.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment