---
title: "Detect HTML character encoding in Go"
url: https://recursiveai.org/onehtml/go/cookbook/detect-html-character-encoding/
description: "Detect an HTML page’s starting character encoding from its byte order mark, HTTP Content-Type header, or meta charset declaration."
version: "1.1.1"
---

# Detect HTML character encoding in Go

**onehtml-go** is a free, open-source HTML parser for Go from Recursive AI, with CSS selectors, computed styles, lossless editing and accessibility checks. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onehtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-go) · [Docs](https://recursiveai.org/onehtml/go/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/go/cookbook/detect-html-character-encoding/detect-html-character-encoding.zip)

Pass the raw bytes to `SniffEncoding` before decoding to see which charset the parser starts with.

```shell
go get recursiveai.org/go/onehtml
```

Requires Go 1.24 or later.

```go
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](https://recursiveai.org/onehtml/go/cookbook/detect-html-character-encoding/detect-html-character-encoding.zip) and `go run .`.

`go run .` prints:

```text
KOI8-R false
```

`false` 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.

```go
bom := []byte("\xef\xbb\xbf")
info = onehtml.SniffEncoding(append(bom, html...))
fmt.Println(info.Encoding, info.Certain, info.BOMLength)
```

## Related recipes

- [Parse an HTML fragment with element context in Go](https://recursiveai.org/onehtml/go/cookbook/parse-html-fragment-in-context/)
- [Get computed CSS styles without a browser in Go](https://recursiveai.org/onehtml/go/cookbook/compute-css-style-without-browser/)

## 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`:

```go
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](https://pkg.go.dev/recursiveai.org/go/onehtml@v1.1.1#SniffEncoding) for the options and precedence.
