Monday, December 9, 2013

I have an internal API that returns io.ReadCloser and I wanted to wrap in a charset decoder. I found the excellent github.com/axgle/mahonia library, but it only provides an io.Reader.

However, I found that's it's quite easy to create a simple pass-through io.ReadCloser with anonymous structs withs anonymous fields:

  rc := struct {
    io.Reader
    io.Closer
  }{mahonia.NewDecoder("latin2").NewReader(r), r}

Thursday, November 28, 2013

Go templates and partials

I was looking around to see how people usually implement partial templates with the standard go templating library.

The usual pattern is to create a layout template which includes a content template, and then each partial template starts with

{{define "content"}}
blah blah
{{end}}

As described in this excellent post the Go template library uses the file base name as template name, so it's very tempting to just use a directive inside the partial template itself to force a particular name.

But I find this boilerplate in each partial very annoying, so I found this work around: