I was trying to print a []uint8
slice in an html template within a <script></script>
block, but it does weird things. I say that, because printing a []int8
behaves as I would expect.
Incase the above play link ever vanishes, the code is:
package main
import (
"html/template"
"os"
)
func main() {
Create := func(name, t string) *template.Template {
return template.Must(template.New(name).Parse(t))
}
t2 := Create("t2", "outside script:\n{{.INT8_ARR}}\n{{.UINT8_ARR}}\n{{.INT8}}\n{{.UINT8}}\n<script>\n{{.INT8_ARR}}\n{{.UINT8_ARR}}\n{{.INT8}}\n{{.UINT8}}\n</script>")
t2.Execute(os.Stdout, struct {
INT8_ARR []int8
UINT8_ARR []uint8
INT8 int8
UINT8 uint8
}{
INT8_ARR: []int8{2, 3, 4},
UINT8_ARR: []uint8{2, 3, 4},
INT8: 5,
UINT8: 6,
})
}
With the output being:
outside script:
[2 3 4]
[2 3 4]
5
6
<script>
[2,3,4]
"AgME"
5
6
</script>
For some reason, it looks to be base64 encoding a uint8 array (the "AgME"
). Any idea what’s going on here? Does html/template think it’s a byte array and attempting to sanitize it?