Package atom provides integer codes (also known as atoms) for a fixed set of frequently occurring HTML strings: tag names and attribute keys such as "p" and "id".
Sharing an atom's name between all elements with the same tag can result in fewer string allocations when tokenizing and parsing HTML. Integer comparisons are also generally faster than string comparisons.
import "golang.org/x/net/html/atom"// Atom is an integer code for a string. The zero value maps to ""
type Atom uint32
func Lookup(s []byte) Atom
func (a Atom) String() stringLookup returns the atom whose name is s. It returns zero if there is no such atom. The lookup is case sensitive.
String returns the atom's name.
// String returns a string whose contents are equal to s
func String(s []byte) stringString returns a string whose contents are equal to s. In that sense, it is equivalent to string(s) but may be more efficient.
The package defines constants for all standard HTML tag names and attribute keys. Some commonly used atoms include:
const (
A Atom = 0x1
Abbr Atom = 0x4
Address Atom = 0x6f107
Area Atom = 0x35004
Article Atom = 0x3f607
Aside Atom = 0x10705
Audio Atom = 0x11505
B Atom = 0x101
Base Atom = 0x3b04
Body Atom = 0x2804
Br Atom = 0x202
Button Atom = 0x19106
Canvas Atom = 0x10306
Caption Atom = 0x22407
Cite Atom = 0x19c04
Class Atom = 0x55805
Code Atom = 0x5ee04
Col Atom = 0x1ab03
Colgroup Atom = 0x1ab08
Data Atom = 0x49904
Datalist Atom = 0x49908
Dd Atom = 0x2bf02
Del Atom = 0x44c03
Details Atom = 0x7207
Dfn Atom = 0x8703
Dialog Atom = 0xbb06
Div Atom = 0x16b03
Dl Atom = 0x5d602
Dt Atom = 0x64002
Em Atom = 0x6e02
Embed Atom = 0x6e05
Fieldset Atom = 0x21908
Figcaption Atom = 0x2210a
Figure Atom = 0x23b06
Footer Atom = 0xf606
Form Atom = 0x26104
H1 Atom = 0x15c02
H2 Atom = 0x56102
H3 Atom = 0x2cd02
H4 Atom = 0x2fc02
H5 Atom = 0x33f02
H6 Atom = 0x34902
Head Atom = 0x32004
Header Atom = 0x32006
Hr Atom = 0x15702
Href Atom = 0x2cf04
Html Atom = 0x5604
I Atom = 0x601
Id Atom = 0x10902
Iframe Atom = 0x2eb06
Img Atom = 0x2f603
Input Atom = 0x44505
Ins Atom = 0x20303
Kbd Atom = 0xb903
Label Atom = 0x5905
Legend Atom = 0x18106
Li Atom = 0xb202
Link Atom = 0x17404
Main Atom = 0x1004
Map Atom = 0x30703
Mark Atom = 0xb604
Meta Atom = 0x4ac04
Meter Atom = 0x9805
Nav Atom = 0x7703
Noscript Atom = 0x3f408
Object Atom = 0x19f06
Ol Atom = 0x1c02
Optgroup Atom = 0x5e08
Option Atom = 0x60e06
Output Atom = 0x49c06
P Atom = 0xc01
Param Atom = 0x4b905
Picture Atom = 0x50407
Pre Atom = 0x50d03
Progress Atom = 0x52008
Q Atom = 0xd101
Rp Atom = 0x53702
Rt Atom = 0x53d02
Ruby Atom = 0x53f04
S Atom = 0x2501
Samp Atom = 0x6804
Script Atom = 0x5f906
Section Atom = 0x6d507
Select Atom = 0x6dc06
Small Atom = 0x6e805
Source Atom = 0x70006
Span Atom = 0x70504
Strong Atom = 0x6db06
Style Atom = 0x70405
Sub Atom = 0x66b03
Summary Atom = 0x70907
Sup Atom = 0x71003
Svg Atom = 0x71303
Table Atom = 0x58505
Tbody Atom = 0x2705
Td Atom = 0x9202
Template Atom = 0x71908
Textarea Atom = 0x34c08
Tfoot Atom = 0xf505
Th Atom = 0x15602
Thead Atom = 0x31f05
Time Atom = 0x4204
Title Atom = 0x11005
Tr Atom = 0xcc02
Track Atom = 0x1ba05
U Atom = 0xb01
Ul Atom = 0xa702
Var Atom = 0x16d03
Video Atom = 0x2e005
Wbr Atom = 0x56c03
// ... and many more
)Note: The value of an atom's particular code is not guaranteed to stay the same between versions of this package. Neither is any ordering guaranteed. The codes are not guaranteed to be dense. The only guarantees are that looking up a string will yield the corresponding atom, calling the atom's String method will return the string, and the atom will be non-zero.
import "golang.org/x/net/html/atom"
func checkTagName(tagBytes []byte) {
// Look up atom from byte slice
a := atom.Lookup(tagBytes)
if a == atom.Div {
fmt.Println("Found a div tag")
}
// Convert atom back to string
tagName := a.String()
fmt.Println("Tag name:", tagName)
// Fast integer comparison
if a == atom.A || a == atom.Button {
fmt.Println("Interactive element")
}
}