Background I was trying to write some GZIP middleware for Go, but ran into a problem as http.DetectContentType() was returning text/plain instead of text/html so I tracked it down to the first text being written was some sort or errant byte, that I am still trying to track down. I know there are lots of ways around it, like setting the ContentType explicitly or using a bytes.Buffer when Executing the template and writing all at once, but I really want to find out what is causing this single byte to be written.
Example Here is a very contrived example that shows the same issue https://play.golang.org/p/SSrWP9jLRq
when run you will see the first thing that is printed is: "LEN BYTES: 1 String: "
Suspects It appears, as the example shows, that this line "{{template "header" .}}" within the content template is the culprit, but why would it be outputting this extra content.
Question Does anybody know where this errant byte is coming from? see the html header, footer and content templates in the example.
Because template content starts immediately following the closing braces in the define directive, the root, header and footer templates start with a newline.
The single newline is written at the start because the template flushes output before invoking a sub-template.
Change the definitions to start with
header = `{{define "header"}}<!DOCTYPE html>
...
content = `{{define "root"}}{{template "header" .}}
to eliminate the extra newlines.
See https://play.golang.org/p/AzD98cD7c0 for a fix.