Search code examples
gogo-html-template

+ in base64 with html/template golang


Salutations fellow gophers.

I need to return data matrix in base64 form in an html template. In template it looks like this. <img src="data:image/png;base64,{{.Datamatrix}}" alt="Datamatrix"/>

I insert my base64 data in .Datamatrix. And all is fine. Base64 is valid and all(checked on different decoders), however when I pass the data to template +(plus) symbols change to &#43

Encoding base64 like so: base64.StdEncoding.EncodeToString(b)

Tried base64.URLEncoding.EncodeToString(b) too, but then even more symbols get messed up.

Passing param as structure field of type string

Tried passing it different ways: as type template.HTML, used template.HTMLEscapeString, template.URLEscapeString, template.JSEscapeString with no result.

Working solution to this problem would be highly appreciated.

Example https://go.dev/play/p/c_s-wPl6dMT

Related issue on github: https://github.com/golang/go/issues/42506

P>S> Changing to text/template is not an option either. In real project I have some more things, that I inject in template, and they simply won't work with text/template lib.


Solution

  • Surprisingly enough text/template solved all issues. And didn't cause any new problems with other injections.

    https://go.dev/play/p/SsZ9j7WeZ5H