### Example UserAgent Struct in Go Source: https://github.com/lumenresearch/uasurfer/blob/master/README.md This is an example of the `UserAgent` struct returned by the `Parse` function, illustrating the structure of parsed browser, OS, and device information. ```go { Browser { BrowserName: BrowserChrome, Version: { Major: 45, Minor: 0, Patch: 2454, }, }, OS { Platform: PlatformMac, Name: OSMacOSX, Version: { Major: 10, Minor: 10, Patch: 5, }, }, DeviceType: DeviceComputer, } ``` -------------------------------- ### Trim Prefix from Browser, OS, and Device Names in Go Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use `StringTrimPrefix()` to get human-readable names without the type prefix, useful for logging and analytics. This example demonstrates summarizing a User-Agent string into a structured format. ```go package main import ( "encoding/json" "fmt" "github.com/LumenResearch/uasurfer" ) type UAInfo struct { Browser string `json:"browser"` OS string `json:"os"` Device string `json:"device"` } func summarize(rawUA string) UAInfo { ua := uasurfer.Parse(rawUA) return UAInfo{ Browser: ua.Browser.Name.StringTrimPrefix(), OS: ua.OS.Name.StringTrimPrefix(), Device: ua.DeviceType.StringTrimPrefix(), } } func main() { uas := []string{ "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1", "Mozilla/5.0 (Linux; Android 11; AFTKRT Build/RS8133.2817N; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/130.0.6723.170 Mobile Safari/537.36", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", } for _, raw := range uas { info := summarize(raw) b, _ := json.Marshal(info) fmt.Println(string(b)) } // {"browser":"Safari","os":"iOS","device":"Phone"} // {"browser":"Chrome","os":"Android","device":"TV"} // {"browser":"GoogleBot","os":"Bot","device":"Computer"} } ``` -------------------------------- ### Parse User-Agent String Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use `Parse` to get browser, OS, and device details from a raw User-Agent string. It returns a populated `*UserAgent` struct. ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { rawUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" ua := uasurfer.Parse(rawUA) fmt.Println("Browser:", ua.Browser.Name) // BrowserChrome fmt.Println("Browser version:", ua.Browser.Version.Major) // 43 fmt.Println("Platform:", ua.OS.Platform) // PlatformMac fmt.Println("OS:", ua.OS.Name) // OSMacOSX fmt.Printf("OS version: %d.%d.%d\n", ua.OS.Version.Major, ua.OS.Version.Minor, ua.OS.Version.Patch) // 10.10.4 fmt.Println("Device:", ua.DeviceType) // DeviceComputer fmt.Println("Is bot:", ua.IsBot()) // false // String representations (trim type prefix) fmt.Println(ua.Browser.Name.StringTrimPrefix()) // "Chrome" fmt.Println(ua.OS.Name.StringTrimPrefix()) // "MacOSX" fmt.Println(ua.DeviceType.StringTrimPrefix()) // "Computer" } ``` -------------------------------- ### Parse User-Agent with Screen Size Hints Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use `ParseWithHints` to provide additional signals like screen size, which can help disambiguate devices like iPads in desktop mode from actual Macs. ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { // iPad in desktop mode — UA string looks like a Mac rawUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" // Without hints — misidentified as a Mac computer uaNoHints := uasurfer.Parse(rawUA) fmt.Println("No hints — OS:", uaNoHints.OS.Name, "Device:", uaNoHints.DeviceType) // No hints — OS: OSMacOSX Device: DeviceComputer // With screen-size hint matching a known iPad resolution hints := &uasurfer.Hints{ ScreenSize: &uasurfer.ScreenSize{Width: 1024, Height: 768}, } uaWithHints := uasurfer.ParseWithHints(rawUA, hints) fmt.Println("With hints — OS:", uaWithHints.OS.Name, "Device:", uaWithHints.DeviceType) // With hints — OS: OSiPadOS Device: DeviceTablet } ``` -------------------------------- ### Parse User Agent with Hints in Go Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use ParseUserAgentWithHints for zero-allocation parsing combined with hints for disambiguation. Provide screen dimensions to help classify devices more accurately. ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func handleRequest(rawUA string, screenW, screenH int) { ua := new(uasurfer.UserAgent) var hints *uasurfer.Hints if screenW > 0 && screenH > 0 { hints = &uasurfer.Hints{ ScreenSize: &uasurfer.ScreenSize{Width: screenW, Height: screenH}, } } ua.Reset() uasurfer.ParseUserAgentWithHints(rawUA, hints, ua) fmt.Printf("Platform: %s, OS: %s, Device: %s\n", ua.OS.Platform.StringTrimPrefix(), ua.OS.Name.StringTrimPrefix(), ua.DeviceType.StringTrimPrefix(), ) } func main() { handleRequest( "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36", 1194, 834, // iPad Pro screen — reclassified as iPad ) // Platform: iPad, OS: iPadOS, Device: Tablet } ``` -------------------------------- ### Compare Version Numbers in Go Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use the Version.Less method for lexicographical three-part version comparison (Major, Minor, Patch). This is useful for implementing conditional logic based on browser or OS version. ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { ua := uasurfer.Parse( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.123", ) ie12 := uasurfer.Version{Major: 12, Minor: 0, Patch: 0} ie11 := uasurfer.Version{Major: 11, Minor: 0, Patch: 0} fmt.Println("Browser:", ua.Browser.Name) // BrowserIE fmt.Println("Version >= IE12:", !ua.Browser.Version.Less(ie12)) // true (Edge 12) fmt.Println("Version >= IE11:", !ua.Browser.Version.Less(ie11)) // true // Practical: serve legacy fallback for IE < 11 if ua.Browser.Name == uasurfer.BrowserIE && ua.Browser.Version.Less(ie11) { fmt.Println("Serving legacy IE content") } else { fmt.Println("Serving modern content") } // → Serving modern content } ``` -------------------------------- ### Parse Source: https://context7.com/lumenresearch/uasurfer/llms.txt Parses a raw User-Agent string and returns a populated *UserAgent struct containing browser, OS, and device information. This is the primary entry point for parsing. ```APIDOC ## Parse ### Description Parses a UA string and returns a new UserAgent struct. ### Method `uasurfer.Parse(rawUA string) *UserAgent` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { rawUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" ua := uasurfer.Parse(rawUA) fmt.Println("Browser:", ua.Browser.Name) fmt.Println("Browser version:", ua.Browser.Version.Major) fmt.Println("Platform:", ua.OS.Platform) fmt.Println("OS:", ua.OS.Name) fmt.Printf("OS version: %d.%d.%d\n", ua.OS.Version.Major, ua.OS.Version.Minor, ua.OS.Version.Patch) fmt.Println("Device:", ua.DeviceType) fmt.Println("Is bot:", ua.IsBot()) } ``` ### Response #### Success Response (200) - **ua** (*UserAgent) - A struct containing parsed User-Agent information. #### Response Example ```json { "Browser": { "Name": "BrowserChrome", "Version": { "Major": 43, "Minor": 0, "Patch": 0 } }, "OS": { "Platform": "PlatformMac", "Name": "OSMacOSX", "Version": { "Major": 10, "Minor": 10, "Patch": 4 } }, "DeviceType": "DeviceComputer", "IsBot": false } ``` ``` -------------------------------- ### Aggregate UA Statistics with uastats CLI Source: https://context7.com/lumenresearch/uasurfer/llms.txt The `uastats` CLI tool processes User-Agent strings from stdin to generate a sorted frequency table of browsers, operating systems, and device types. It utilizes the high-throughput pattern of `Reset()` and `ParseUserAgent()`. ```bash # Build the tool go build -o uastats ./cmd/uastats # Feed a log file (one UA per line) and get aggregated stats cat access.log | awk '{print $12}' | tr -d '"' | ./uastats # Example output: # Read 192792 useragents # # Browsers # BrowserChrome 142301 (73.81%) # BrowserSafari 28415 (14.74%) # BrowserFirefox 10244 (05.31%) # BrowserIE 5109 (02.65%) # ... # # Operating Systems # OSAndroid 71220 (36.94%) # OSWindows 55310 (28.69%) # OSiOS 28991 (15.04%) # OSMacOSX 21044 (10.91%) # ... # # Device Types # DevicePhone 98302 (50.99%) # DeviceComputer 74881 (38.84%) # DeviceTablet 17441 (09.05%) # DeviceTV 2168 (01.12%) ``` -------------------------------- ### Zero-Allocation User-Agent Parsing Source: https://context7.com/lumenresearch/uasurfer/llms.txt Use `ParseUserAgent` with a pre-allocated `UserAgent` struct and `Reset()` for allocation-free parsing in high-throughput scenarios. Ensure `Reset()` is called before each reuse. ```go package main import ( "bufio" "fmt" "os" "github.com/LumenResearch/uasurfer" ) func main() { ua := new(uasurfer.UserAgent) // allocate once scanner := bufio.NewScanner(os.Stdin) // read UA strings from stdin for scanner.Scan() { ua.Reset() // must reset before reuse uasurfer.ParseUserAgent(scanner.Text(), ua) fmt.Printf("browser=%-20s os=%-12s device=%s\n", ua.Browser.Name.StringTrimPrefix(), ua.OS.Name.StringTrimPrefix(), ua.DeviceType.StringTrimPrefix(), ) } } ``` -------------------------------- ### ParseWithHints Source: https://context7.com/lumenresearch/uasurfer/llms.txt Parses a User-Agent string with additional screen-size hints to improve disambiguation, particularly for devices like iPads in desktop mode. ```APIDOC ## ParseWithHints ### Description Parses with additional screen-size hints. ### Method `uasurfer.ParseWithHints(rawUA string, hints *Hints) *UserAgent` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **rawUA** (string) - The raw User-Agent string to parse. - **hints** (*uasurfer.Hints) - An optional struct containing additional signals like screen size for finer-grained disambiguation. ### Request Example ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { rawUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" // Without hints — misidentified as a Mac computer uaNoHints := uasurfer.Parse(rawUA) fmt.Println("No hints — OS:", uaNoHints.OS.Name, "Device:", uaNoHints.DeviceType) // With screen-size hint matching a known iPad resolution hints := &uasurfer.Hints{ ScreenSize: &uasurfer.ScreenSize{Width: 1024, Height: 768}, } uaWithHints := uasurfer.ParseWithHints(rawUA, hints) fmt.Println("With hints — OS:", uaWithHints.OS.Name, "Device:", uaWithHints.DeviceType) } ``` ### Response #### Success Response (200) - **ua** (*UserAgent) - A struct containing parsed User-Agent information, potentially disambiguated by hints. #### Response Example ```json { "Browser": { "Name": "BrowserChrome", "Version": { "Major": 87, "Minor": 0, "Patch": 0 } }, "OS": { "Platform": "PlatformMac", "Name": "OSiPadOS", "Version": { "Major": 11, "Minor": 1, "Patch": 0 } }, "DeviceType": "DeviceTablet", "IsBot": false } ``` ``` -------------------------------- ### ParseUserAgent Source: https://context7.com/lumenresearch/uasurfer/llms.txt Populates a caller-supplied *UserAgent struct, enabling zero-allocation parsing in high-throughput scenarios. The caller must call Reset() before reuse. ```APIDOC ## ParseUserAgent ### Description Parses into a pre-allocated UserAgent (zero-allocation hot path). ### Method `uasurfer.ParseUserAgent(rawUA string, ua *UserAgent)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **rawUA** (string) - The raw User-Agent string to parse. - **ua** (*UserAgent) - A pointer to a UserAgent struct to be populated. Must be reset before reuse. ### Request Example ```go package main import ( "bufio" "fmt" "os" "github.com/LumenResearch/uasurfer" ) func main() { ua := new(uasurfer.UserAgent) // allocate once scanner := bufio.NewScanner(os.Stdin) // read UA strings from stdin for scanner.Scan() { ua.Reset() // must reset before reuse uasurfer.ParseUserAgent(scanner.Text(), ua) fmt.Printf("browser=%%-20s os=%%-12s device=%%s\n", ua.Browser.Name.StringTrimPrefix(), ua.OS.Name.StringTrimPrefix(), ua.DeviceType.StringTrimPrefix(), ) } } ``` ### Response #### Success Response (200) - **ua** (*UserAgent) - The provided UserAgent struct is populated with parsed information. #### Response Example (No specific response example provided as the function modifies the input struct in-place. The example demonstrates usage.) ``` -------------------------------- ### Parse User Agent String in Go Source: https://github.com/lumenresearch/uasurfer/blob/master/README.md Use the `Parse` function to process a raw HTTP User-Agent string. It returns a `UserAgent` struct containing parsed details and the original UA string. ```go myUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" ua, uaString := uasurfer.Parse(myUA) ``` -------------------------------- ### Check if User Agent is a Bot in Go Source: https://context7.com/lumenresearch/uasurfer/llms.txt The IsBot method efficiently checks if a parsed user agent string belongs to a known crawler or bot. It also returns true if the platform or OS is identified as a bot type. ```go package main import ( "fmt" "github.com/LumenResearch/uasurfer" ) func main() { botStrings := []string{ "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)", "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)", "Twitterbot/1.0", "Mozilla/5.0 (Windows NT 10.0) Chrome/90.0 Safari/537.36", // real user } for _, s := range botStrings { ua := uasurfer.Parse(s) fmt.Printf("% -80s → bot=% -5v browser=%s\n", s[:min(len(s), 60)], ua.IsBot(), ua.Browser.Name.StringTrimPrefix()) } // Googlebot/2.1... → bot=true browser=GoogleBot // bingbot/2.0... → bot=true browser=BingBot // facebookexternal → bot=true browser=FacebookBot // Twitterbot/1.0 → bot=true browser=TwitterBot // Windows NT 10.0 → bot=false browser=Chrome } func min(a, b int) int { if a < b { return a } return b } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.