Pass the raw bytes to SniffEncoding before decoding to see which charset the parser starts with.
go get recursiveai.org/go/onehtmlRequires Go 1.24 or later.
package main import ( "fmt" "recursiveai.org/go/onehtml" ) func main() { html := []byte(`<meta charset="koi8-r">`) info := onehtml.SniffEncoding(html) fmt.Println(info.Encoding, info.Certain) }
An unknown encoding label is ignored. When nothing else decides, the default is windows-1252 and the result is tentative.
Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onehtml, then go run .; or unzip the example and go run ..
go run . prints:
KOI8-R falsefalse means the encoding is tentative: a later <meta> declaration can still change it during parsing.
Variations
Put a UTF-8 byte order mark in front of the same HTML. Add this inside main; it prints UTF-8 true 3 because the marker takes precedence over the meta label.
bom := []byte("\xef\xbb\xbf") info = onehtml.SniffEncoding(append(bom, html...)) fmt.Println(info.Encoding, info.Certain, info.BOMLength)
Related recipes
The short answer
Call onehtml.SniffEncoding on the original bytes. Read Encoding for the starting charset, Certain to see whether a later <meta> can override it, and BOMLength for the size of a byte order mark. If you’re already parsing the page and want the encoding actually used, get it from doc.Encoding() after a successful parse.
How do I detect the charset of an HTML page in Go?
Keep the bytes as they arrived, not as text you’ve already decoded. For an HTTP response, resp is the *http.Response and body is the raw []byte read from resp.Body:
contentType := onehtml.WithContentType(resp.Header.Get("Content-Type")) info := onehtml.SniffEncoding(body, contentType) fmt.Println(info.Encoding, info.Certain)
Pass contentType to ParseBytes too if you then parse body.
The default is windows-1252. Use WithDefaultEncoding to pick a different fallback when there’s no byte order mark, no recognized HTTP charset, and no <meta> charset declaration. Use WithEncoding to set an explicit encoding that wins over both the HTTP charset and the page’s declaration. A byte order mark still wins over WithEncoding, as it does over the koi8-r declaration in the variation.
Why can the detected encoding differ from the parsed encoding?
SniffEncoding looks for a meta declaration in the first 1,024 bytes by default. Its answer for a meta declaration or a fallback is tentative. The parser can encounter a declaration later and restart decoding before finishing. A recognized transport charset, an explicit override, or a BOM is certain, so a declaration further down the page cannot replace it.
This is why a preflight log that prints SniffEncoding should describe it as the starting encoding. It’s useful for diagnosing which hint the parser can see early; it is not the final diagnosis of garbled text. If you already need the tree, let onehtml-go do the decoding and log the resulting Document.Encoding() rather than decoding the body yourself. That keeps the application from interpreting legacy text twice. See the pinned SniffEncoding reference for the options and precedence.