Quick Start
Install the library, run a first extraction, then pick a configuration preset and output format for your case.
Installation
go get github.com/cybergodev/htmlRequires Go 1.26+.
Basic Extraction
Extract content from HTML bytes:
package main
import (
"fmt"
"log"
"github.com/cybergodev/html"
)
func main() {
data := []byte(`<html>
<head><title>Go Tutorial</title></head>
<body>
<article>
<h1>Getting Started with Go</h1>
<p>Go is a statically typed compiled language.</p>
<img src="gopher.png" alt="Gopher" />
<a href="https://go.dev">Go Official Site</a>
</article>
</body>
</html>`)
result, err := html.Extract(data)
if err != nil {
log.Fatal(err)
}
fmt.Println("Title:", result.Title)
fmt.Println("Text:", result.Text)
fmt.Println("Images:", len(result.Images))
fmt.Println("Links:", len(result.Links))
fmt.Println("Words:", result.WordCount)
}Output:
Title: Go Tutorial
Text: # Getting Started with Go
Go is a statically typed compiled language.
Go Official Site
Images: 1
Links: 1
Words: 14WordCount splits on whitespace and counts each CJK character as one more word. ReadingTime is folded per script: space-separated languages at 200 words per minute, CJK characters at 350 characters per minute. Heading tags are written into Text with Markdown ATX markers (# through ###### ); the ATX markers themselves are not counted as words, but the heading text still counts.
Extract from File
result, err := html.ExtractFromFile("page.html")
if err != nil {
log.Fatal(err)
}Using Configuration
Customize extraction behavior with Config:
cfg := html.MarkdownConfig()
p, err := html.New(cfg)
if err != nil {
log.Fatal(err)
}
defer p.Close()
result, err := p.Extract(data)Configuration Presets
| Preset | Function | Description |
|---|---|---|
| Default | DefaultConfig() | Balanced configuration for general use |
| Text | TextOnlyConfig() | Keeps no images, links, videos, or audio; the Include* switches of ExtractAllLinks are unaffected |
| Markdown | MarkdownConfig() | Sets InlineImageFormat and InlineLinkFormat to markdown; ExtractToMarkdown always emits Markdown and does not need it |
| High Security | HighSecurityConfig() | Tightens input size, depth, and timeout limits and enables full audit |
Output Formats
// Plain text
text, err := html.ExtractText(data)
// Markdown
md, err := html.ExtractToMarkdown(data)
// JSON
jsonBytes, err := html.ExtractToJSON(data)ExtractText calls Extract internally and returns .Text, so it goes through the same processing path as Extract.
Context Support
Extraction functions have variants with the *WithContext suffix (for example ExtractWithContext, ExtractTextWithContext) that support cancellation and timeouts:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
result, err := html.ExtractWithContext(ctx, data)When the library's own ProcessingTimeout expires you get ErrProcessingTimeout; when the caller's context expires the original context.DeadlineExceeded is returned unchanged. See the FAQ for the full comparison.
Usage Notes
Concurrency Safety
Processor instances are concurrency-safe and can be shared across goroutines:
p, err := html.New(html.DefaultConfig())
if err != nil {
log.Fatal(err)
}
defer p.Close()
// Safe to call from multiple goroutines
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(u string) {
defer wg.Done()
result, err := p.Extract(fetchHTML(u))
// ...
}(url)
}
wg.Wait()Package-level functions are also concurrency-safe: they take a Processor from an internal pool; pooled instances have the cache disabled, and statistics are reset and the audit log cleared on return, so there is no caching benefit. Use html.New(cfg) for a long-lived Processor when you need cache hits.
Encoding Detection
The library decides the encoding from a BOM, a <meta charset> declaration, and byte statistics:
// Auto-detection works when the encoding is declared or the bytes are conclusive
result, err := html.Extract(pageData)
// When the source encoding is known, set it explicitly — the most reliable option
cfg := html.DefaultConfig()
cfg.Encoding = "gbk"Supported encodings include UTF-8, GBK, Shift_JIS, EUC-JP, Windows-1252, and 30+ others. A GBK or Shift_JIS page without <meta charset> is decoded as windows-1252, producing mojibake with no error. Set Config.Encoding explicitly in production. See Encoding Detection.
Next Steps
- Content Extraction - Extraction pipeline and article detection
- Reader Mode - Whole-page sectioned output, suited to LLM input
- Output Formats - Choose the right output format for your case
- Processor Cache & Reuse - Performance tuning for high-frequency calls
- Cheat Sheet - Common API quick reference