useragent

Go
BSD-3-Clause

A Go package for parsing and analyzing user agent strings

Installation

$ go get packages.infobits.io/go/useragent

useragent

CI Go Reference

A lightweight Go package for parsing user agent strings. Detects browsers, operating systems, devices, device types, and bots. Zero dependencies.

Installation

go get github.com/infobits-io/useragent

Usage

package main

import (
    "fmt"

    "github.com/infobits-io/useragent"
)

func main() {
    ua := useragent.Parse("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")

    fmt.Println(ua.Browser())         // Chrome
    fmt.Println(ua.OperatingSystem()) // windows
    fmt.Println(ua.Device())          // Windows 10
    fmt.Println(ua.DeviceType())      // desktop
    fmt.Println(ua.IsBot(true))       // false
}

API

Parse(userAgent string) *UserAgent

Parses a user agent string and returns a *UserAgent with detected browser, OS, device, and bot information.

UserAgent methods

MethodReturn typeDescription
UserAgent()stringOriginal user agent string
Browser()stringDetected browser name
OperatingSystem()stringDetected operating system
Device()stringDetected device
DeviceType()string"desktop", "mobile", or "tablet"
IsBot(includeBrowser bool)boolWhether the user agent is a bot
IsValid()boolWhether browser, OS, and device are all recognized
IsBrowserValid()boolWhether the browser is recognized
IsOperatingSystemValid()boolWhether the OS is recognized
IsDeviceValid()boolWhether the device is recognized
IsMobile()boolWhether the device type is mobile
IsTablet()boolWhether the device type is tablet
IsDesktop()boolWhether the device type is desktop
IsWindows()boolWhether the OS is Windows
IsLinux()boolWhether the OS is Linux
IsMacOS()boolWhether the OS is macOS
IsAndroid()boolWhether the OS is Android
IsIOS()boolWhether the OS is iOS

Supported Browsers

Chrome, Safari, Firefox, Edge, Opera, Brave, DuckDuckGo, Samsung Internet, UC Browser, Vivaldi, Tor Browser, Internet Explorer, and more. See useragent.go for the full list.

Supported Bots

Googlebot, Bingbot, Baidu, Yandex, DuckDuckBot, Facebook, Twitter, LinkedIn, Instagram, ChatGPT, OpenAI, Ahrefs, SEMRush, and more. See useragent.go for the full list.

License

BSD 3-Clause