Skip to content

Instantly share code, notes, and snippets.

@asemt
Created January 12, 2016 14:40
Show Gist options
  • Save asemt/54b6ff0024b9d1fe8b2a to your computer and use it in GitHub Desktop.
Save asemt/54b6ff0024b9d1fe8b2a to your computer and use it in GitHub Desktop.
Stupid simple Go template example
/*
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