Skip to content

Instantly share code, notes, and snippets.

@ytnobody
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnobody/d2c49007191f2a558b5f to your computer and use it in GitHub Desktop.
Save ytnobody/d2c49007191f2a558b5f to your computer and use it in GitHub Desktop.
etude for golang
package docdami
import (
"io/ioutil"
"log"
"fmt"
"github.com/lestrrat/go-xslate"
)
type Docdami struct {
Xslate *xslate.Xslate
}
func New () (*Docdami, error) {
xt, err := xslate.New();
if err != nil {
log.Fatalf("Failed to create xslate: %s", err)
}
d := &Docdami{
Xslate: xt,
}
return d, nil
}
func (d *Docdami) Run (file string) (string, error) {
tmpl, err := ioutil.ReadFile(file);
if err != nil {
log.Fatalf("Failed to read a file: %s", err)
}
output, err := d.Xslate.RenderString(string(tmpl), xslate.Vars { "name": "Docdami" })
if err != nil {
log.Fatalf("Failed to render template: %s", err)
}
str := fmt.Sprintf("%s", output)
return str, err
}
Hello, [% name %] World!
package docdami
import (
"testing"
)
func TestRun(t *testing.T) {
d, err := New()
if err != nil {
t.Errorf("caught error : %s", err);
}
str, err := d.Run("foo.tt")
expected := "Hello, Docdami World!\n"
if err != nil {
t.Errorf("caught error : %s", err);
}
if str != expected {
t.Errorf("got %s\nwant %s", str, expected);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment