useragent
A dependency-free Go library for parsing browser, OS, device, and bot data out of User-Agent strings.
Repository Health
Technical Analysis
useragent is a lightweight Go package for parsing HTTP User-Agent strings into structured data: browser name and version, operating system and version, and device type (mobile, tablet, desktop, or bot). It exposes a single Parse function built on a hand-written, deterministic tokenizer rather than a large external regex database, so it stays fast under heavy production traffic.
Beyond the raw fields, the package adds convenience methods like IsChrome(), IsAndroid(), and IsGooglebot() for common comparisons, plus a VersionNo struct that breaks version strings into Major/Minor/Patch integers for numeric comparisons such as checking whether a browser is below a minimum supported version. The author runs it in production on high-traffic sites, including their own analytics service, and continues to extend bot and browser coverage from community-reported edge cases.
What You Get
- A
Parse(userAgent string) UserAgentfunction returning a struct with Name, Version, OS, OSVersion, Device, and boolean Mobile/Tablet/Desktop/Bot flags - Shorthand boolean methods like
IsChrome(),IsAndroid(),IsGooglebot(), andIsSafari()for readable conditional checks instead of raw string comparisons - A
VersionNostruct with Major/Minor/Patch integers plusVersionNoShort()/VersionNoFull()andOSVersionNoShort()/OSVersionNoFull()helpers for numeric version comparisons - Built-in detection for major bots (Googlebot, Bingbot, YandexBot, Applebot, Twitterbot, AdsBot) and in-app browsers (Facebook, Instagram, TikTok)
Common Use Cases
- Analytics pipelines - tagging incoming requests with browser/OS/device type for dashboards without a heavyweight geo/UA service
- Bot filtering - excluding crawler and bot traffic from real-user metrics using the
Botflag andIsGooglebot()/IsTwitterbot()helpers - Feature gating by browser version - using
VersionNo.Majorto redirect or warn users on outdated browsers - Responsive server-side rendering - branching HTML output for mobile vs. tablet vs. desktop using the
Mobile/Tablet/Desktopflags
Under The Hood
Architecture
The package is a single flat useragent module split across three files: ua.go (the tokenizer and Parse entry point), is.go (boolean convenience methods on UserAgent), and version.go (VersionNo parsing/formatting). There is no layering, dependency injection, or exported interface beyond Parse itself — the private tokenizer (parse, properties, property) walks the raw string once into a flat list of key/value tokens, and Parse then runs two large sequential switch statements (OS lookup, then browser/bot name lookup) against those tokens, falling back to helper functions like findBestMatch, findAndroidDevice, and findMacOSVersion for ambiguous cases. Data flows one-directionally from raw string to tokens to the UserAgent struct within a single call; because both switch statements and the resolution helpers share assumptions about token shape, changing the tokenizer’s output format would ripple through nearly the whole file.
Tech Stack
The implementation uses only the Go standard library (bytes, regexp, strings, strconv, fmt) with zero third-party dependencies — go.sum is empty. go.mod targets Go 1.14 and there is no build tooling beyond go build/go test, and no CI workflow config in the repository.
Code Quality
Testing relies on two table-driven test functions (TestParse, TestSingle) backed by a roughly 26KB table of real-world User-Agent strings covering dozens of browsers, operating systems, devices, and bots, run via the standard testing package with no additional assertion library. Error handling is minimal by design: parseVersion silently returns a zero-value VersionNo on a non-numeric segment rather than surfacing an error, which is a reasonable tradeoff for a parser that must never fail on malformed input, but means version parsing failures are invisible to callers. There is no linter or formatter configuration checked in, and naming is consistent and idiomatic Go throughout.
What Makes It Unique
Rather than matching against a large external database of regex signatures (the common approach used by many UA-parsing libraries), this package hand-tokenizes the string once and classifies it with explicit switch/case logic tuned against real-world traffic, prioritizing raw parsing speed and a small dependency-free footprint over exhaustive device-database coverage. The VersionNo struct is a small but practical differentiator, giving callers numeric version components without needing to hand-parse the Version string themselves.
Used by 6 apps in this directory
CasaOS
Hosting Control Panel · File Storage
Your simple, elegant personal cloud OS for home data and apps
Hanko
Security · Authentication
Open source, self-hostable authentication platform with passkeys, SAML SSO, and OAuth — the privacy-first alternative to Auth0 and Clerk.
Navidrome
File Storage
Run your own personal Spotify — stream your entire music collection from any device, anywhere, forever.
seonaut
Marketing · Developer Tools
Self-hosted SEO auditing that crawls your entire website and surfaces critical issues — broken links, duplicate meta tags, redirect loops, and more — before they hurt your rankings.
Uptrace
Monitoring · Devops
Unified open-source APM that collects OpenTelemetry traces, metrics, and logs into a single self-hosted platform backed by ClickHouse.
Wakapi
Developer Tools · Analytics
Self-hosted WakaTime-compatible coding statistics backend that gives developers full control over their coding activity data.