### Install golang-wurfl Package Source: https://github.com/wurfl/golang-wurfl/blob/main/README.md This command installs the golang-wurfl package using the Go build tool. Ensure you have Go installed and configured correctly. This is the first step to include the library in your project. ```go go get -u github.com/WURFL/golang-wurfl ``` -------------------------------- ### Configure WURFL Engine Attributes in Golang Source: https://context7.com/wurfl/golang-wurfl/llms.txt Shows how to set and get WURFL engine attributes in Go, such as configuring the capability fallback cache behavior for performance optimization. This example also demonstrates how to enable logging for debugging purposes. Requires libwurfl version 1.12.9.3+ for certain attributes. ```go package main import ( "fmt" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, _ := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") defer wengine.Destroy() // Set capability fallback cache (requires libwurfl 1.12.9.3+) // Options: WurflAttrCapabilityFallbackCacheDefault, Disabled, Limited err := wengine.SetAttr(wurfl.WurflAttrCapabilityFallbackCache, wurfl.WurflAttrCapabilityFallbackCacheLimited) if err != nil { fmt.Printf("Error setting attribute: %v\n", err) } // Get current attribute value value, err := wengine.GetAttr(wurfl.WurflAttrCapabilityFallbackCache) if err != nil { fmt.Printf("Error getting attribute: %v\n", err) } fmt.Printf("Fallback Cache Setting: %d\n", value) // Enable logging for debugging wengine.SetLogPath("/var/log/wurfl.log") } ``` -------------------------------- ### Configure Automatic WURFL Data Updater in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Sets up an automatic updater for WURFL data files, ensuring device detection remains current. This involves configuring the update URL, frequency, timeouts, and log path, then starting the updater in the background. ```go package main import ( "fmt" "os" "time" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } defer wengine.Destroy() wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" // Configure updater if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil { fmt.Printf("Error setting updater URL: %v\n", err) os.Exit(1) } // Set update frequency (daily or weekly) wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily) // Optional: Set custom timeouts (connection, data transfer in milliseconds) wengine.SetUpdaterDataURLTimeout(30000, 60000) // Optional: Set updater log path wengine.SetUpdaterLogPath("/var/log/wurfl_updater.log") // Start background updater thread if err := wengine.UpdaterStart(); err != nil { fmt.Printf("Error starting updater: %v\n", err) } fmt.Println("Updater started, will check for updates daily") // Or run once and wait for completion // wengine.UpdaterRunonce() // Application runs... time.Sleep(time.Hour) // Stop updater before exit wengine.UpdaterStop() } ``` -------------------------------- ### Get ORTB2 Device Type in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Retrieves the OpenRTB 2.6 device type classification for a given user-agent. This is useful for programmatic advertising integrations. The function first looks up the device and then calls the ORTB2GetDevicetype method. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, _ := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") defer wengine.Destroy() ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36" device, _ := wengine.LookupUserAgent(ua) defer device.Destroy() // Get ORTB2 device type deviceType, err := device.ORTB2GetDevicetype() if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } // ORTB2 Device Types: // 0 = Bot/Spider/Crawler (ScientiaMobile extension) // 1 = Mobile/Tablet - General // 2 = Personal Computer // 3 = Connected TV // 4 = Phone // 5 = Tablet // 6 = Connected Device // 7 = Set Top Box // 8 = OOH Device deviceTypes := map[int]string{ wurfl.ORTB2DeviceTypeBot: "Bot", wurfl.ORTB2DeviceTypeMobile: "Mobile", wurfl.ORTB2DeviceTypePersonalComputer: "PC", wurfl.ORTB2DeviceTypeConnectedTV: "Connected TV", wurfl.ORTB2DeviceTypePhone: "Phone", wurfl.ORTB2DeviceTypeTablet: "Tablet", wurfl.ORTB2DeviceTypeConnectedDevice: "Connected Device", wurfl.ORTB2DeviceTypeSetTopBox: "Set Top Box", wurfl.ORTB2DeviceTypeOOH: "OOH Device", } fmt.Printf("ORTB2 Device Type: %d (%s)\n", deviceType, deviceTypes[deviceType]) } ``` -------------------------------- ### Retrieve Device Capabilities as Integers with Golang WURFL Source: https://context7.com/wurfl/golang-wurfl/llms.txt Demonstrates how to fetch device capabilities, such as screen dimensions and pixel density, directly as integer values using the `GetCapabilityAsInt` and `GetVirtualCapabilityAsInt` methods. This is useful for numerical comparisons and calculations. Requires the WURFL data file. ```go package main import ( "fmt" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, _ := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") defer wengine.Destroy() ua := "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1" device, _ := wengine.LookupUserAgent(ua) defer device.Destroy() // Get static capabilities as integers width, err := device.GetCapabilityAsInt("resolution_width") if err != nil { fmt.Printf("Error getting width: %v\n", err) } height, err := device.GetCapabilityAsInt("resolution_height") if err != nil { fmt.Printf("Error getting height: %v\n", err) } fmt.Printf("Resolution: %dx%d\n", width, height) // Get virtual capabilities as integers pixelDensity, _ := device.GetVirtualCapabilityAsInt("pixel_density") fmt.Printf("Pixel Density: %d\n", pixelDensity) } ``` -------------------------------- ### Download and Use WURFL Engine in Go Source: https://github.com/wurfl/golang-wurfl/blob/main/README.md This Go code snippet demonstrates how to download the WURFL data file, create a WURFL engine instance, and perform device lookups using a user agent string. It includes error handling for download and engine creation, and shows how to retrieve device capabilities. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { // Replace this with your own WURFL updater URL wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" fmt.Println("Downloading WURFL file ...") err := wurfl.Download(wurflUpdaterURL, ".") if err != nil { fmt.Printf("Error downloading WURFL file: %v\n", err) os.Exit(1) } fmt.Println("WURFL file downloaded successfully") wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error creating WURFL engine: %v\n", err) os.Exit(1) } fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo()) c := wengine.GetAllCaps() fmt.Println("Capabilities available = ", c) ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36" device, err := wengine.LookupUserAgent(ua) if err != nil { fmt.Printf("Error in lookup: %v", err) os.Exit(1) } deviceid, err := device.GetDeviceID() if err != nil { fmt.Printf("Error in GetDeviceID: %v", err) os.Exit(1) } fmt.Println(deviceid) cap, _ := device.GetStaticCap("model_name") fmt.Printf("model_name = %s\n", cap) vcap, _ := device.GetVirtualCap("is_android") fmt.Printf("is_android = %s\n", vcap) if wengine.IsUserAgentFrozen(ua) { fmt.Printf("UA %s is frozen. Sec-Ch-Ua headers are necessary for correct device identification.\n", ua) } device.Destroy() wengine.Destroy() } ``` -------------------------------- ### Initialize WURFL Engine in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Creates and configures the WURFL detection engine using a local data file. It supports optional patches, capability filtering, and LRU cache configuration for optimized performance. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error creating WURFL engine: %v\n", err) os.Exit(1) } defer wengine.Destroy() fmt.Println("Engine loaded") fmt.Println("API Version:", wengine.GetAPIVersion()) fmt.Println("WURFL Info:", wengine.GetInfo()) fmt.Println("Last Load Time:", wengine.GetLastLoadTime()) fmt.Println("Last Updated:", wengine.GetLastUpdated()) caps := wengine.GetAllCaps() fmt.Println("Static Capabilities available:", len(caps)) vcaps := wengine.GetAllVCaps() fmt.Println("Virtual Capabilities available:", len(vcaps)) } ``` -------------------------------- ### Download WURFL Data File in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Downloads the required WURFL data file from a remote URL to a local directory. This is a prerequisite step for initializing the WURFL detection engine. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" err := wurfl.Download(wurflUpdaterURL, ".") if err != nil { fmt.Printf("Error downloading WURFL file: %v\n", err) os.Exit(1) } fmt.Println("WURFL file downloaded successfully") } ``` -------------------------------- ### Handle WURFL Errors with Golang Source: https://context7.com/wurfl/golang-wurfl/llms.txt Illustrates robust error handling for WURFL operations in Go. It shows how to use `errors.Is()` to check for specific WURFL errors like `ErrFileNotFound`, `ErrUnableToAllocateMemory`, `ErrCapabilityNotFound`, and `ErrVirtualCapabilityNotAvailable`. Proper error checking is crucial for stable application behavior. ```go package main import ( "errors" "fmt" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { if errors.Is(err, wurfl.ErrFileNotFound) { fmt.Println("WURFL data file not found") } else if errors.Is(err, wurfl.ErrUnableToAllocateMemory) { fmt.Println("Memory allocation failed") } else { fmt.Printf("Engine creation failed: %v\n", err) } return } defer wengine.Destroy() device, err := wengine.LookupUserAgent("test") if err != nil { fmt.Printf("Lookup error: %v\n", err) return } defer device.Destroy() // Check for capability errors _, err = device.GetStaticCap("nonexistent_capability") if err != nil { if errors.Is(err, wurfl.ErrCapabilityNotFound) { fmt.Println("Capability does not exist") } } // Check for virtual capability errors _, err = device.GetVirtualCap("unlicensed_vcap") if err != nil { if errors.Is(err, wurfl.ErrVirtualCapabilityNotAvailable) { fmt.Println("Virtual capability not licensed") } } } ``` -------------------------------- ### Lookup Device with Header Map in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Performs device detection using a map of header names and values, suitable for processing headers from diverse sources. It initializes the WURFL engine and constructs a map containing User-Agent and Client Hints. The function then looks up the device and retrieves various capabilities. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } defer wengine.Destroy() // Build header map with User-Agent and Client Hints ihmap := make(map[string]string) ihmap["User-Agent"] = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36" ihmap["Sec-CH-UA-Platform"] = "Android" ihmap["Sec-CH-UA-Platform-Version"] = `"13.0.0"` ihmap["Sec-CH-UA"] = `"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"` ihmap["Sec-CH-UA-Mobile"] = "?1" ihmap["Sec-Ch-Ua-Model"] = `"SM-S135DL"` ihmap["Sec-Ch-Ua-Full-Version"] = `"126.0.6478.71"` device, err := wengine.LookupWithImportantHeaderMap(ihmap) if err != nil { fmt.Printf("Error in lookup: %v\n", err) os.Exit(1) } defer device.Destroy() deviceID, _ := device.GetDeviceID() fmt.Println("Device ID:", deviceID) completeName, _ := device.GetVirtualCap("complete_device_name") fmt.Println("Complete Device Name:", completeName) // Get multiple capabilities at once caps := []string{"marketing_name", "brand_name", "device_os", "resolution_width", "resolution_height"} capValues, _ := device.GetStaticCaps(caps) for k, v := range capValues { fmt.Printf("%s = %s\n", k, v) } } ``` -------------------------------- ### Lookup Device from HTTP Request in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Detects a device directly from an http.Request object, automatically extracting headers like User-Agent and Client Hints. It initializes the WURFL engine and sets up an HTTP handler to process incoming requests. The function returns device capabilities and header quality. ```go package main import ( "fmt" "log" "net/http" wurfl "github.com/WURFL/golang-wurfl" ) var wengine *wurfl.Wurfl func main() { var err error wengine, err = wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { log.Fatalf("Error creating WURFL engine: %v", err) } defer wengine.Destroy() http.HandleFunc("/detect", detectHandler) log.Fatal(http.ListenAndServe(":8080", nil)) } func detectHandler(w http.ResponseWriter, r *http.Request) { device, err := wengine.LookupRequest(r) if err != nil { http.Error(w, fmt.Sprintf("Lookup error: %v", err), http.StatusInternalServerError) return } defer device.Destroy() // Check header quality for Client Hints headerQuality, _ := wengine.GetHeaderQuality(r) fmt.Fprintf(w, "Header Quality: %s\n", headerQuality) // None, Basic, or Full isRobot, _ := device.GetVirtualCap("is_robot") deviceOS, _ := device.GetVirtualCap("advertised_device_os") browser, _ := device.GetVirtualCap("advertised_browser") fmt.Fprintf(w, "Is Robot: %s\n", isRobot) fmt.Fprintf(w, "Device OS: %s\n", deviceOS) fmt.Fprintf(w, "Browser: %s\n", browser) } ``` -------------------------------- ### Lookup Device by ID in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Retrieves device information using a known WURFL device ID. This method is useful when the device identifier is already known. It initializes the WURFL engine, performs the lookup, and then extracts static capabilities and hierarchy information. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } defer wengine.Destroy() // Lookup by device ID directly device, err := wengine.LookupDeviceID("samsung_sm_s135dl_ver1") if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } defer device.Destroy() modelName, _ := device.GetStaticCap("model_name") fmt.Println("Model Name:", modelName) // Get device hierarchy information rootID := device.GetRootID() parentID := device.GetParentID() isRoot := device.IsRoot() fmt.Printf("Root ID: %s, Parent ID: %s, Is Root: %v\n", rootID, parentID, isRoot) } ``` -------------------------------- ### Lookup Device by User-Agent in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Performs device detection by passing a user-agent string to the WURFL engine. Returns a device handle used to query static and virtual capabilities. ```go package main import ( "fmt" "os" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } defer wengine.Destroy() ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36" device, err := wengine.LookupUserAgent(ua) if err != nil { fmt.Printf("Error in lookup: %v\n", err) os.Exit(1) } defer device.Destroy() deviceID, _ := device.GetDeviceID() fmt.Println("Device ID:", deviceID) modelName, _ := device.GetStaticCap("model_name") brandName, _ := device.GetStaticCap("brand_name") fmt.Printf("Model: %s, Brand: %s\n", modelName, brandName) isAndroid, _ := device.GetVirtualCap("is_android") formFactor, _ := device.GetVirtualCap("form_factor") fmt.Printf("Is Android: %s, Form Factor: %s\n", isAndroid, formFactor) matchType := device.GetMatchType() fmt.Printf("Match Type: %d\n", matchType) } ``` -------------------------------- ### Check Frozen User-Agent in Go Source: https://context7.com/wurfl/golang-wurfl/llms.txt Detects if a user-agent string is 'frozen' or reduced, indicating the need for User-Agent Client Hints. This function requires the WURFL engine to be initialized with a WURFL data file. ```go package main import ( "fmt" wurfl "github.com/WURFL/golang-wurfl" ) func main() { wengine, _ := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") defer wengine.Destroy() // Modern Chrome on Android uses frozen/reduced user-agents ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36" if wengine.IsUserAgentFrozen(ua) { fmt.Println("User-Agent is frozen - Sec-CH-UA headers are needed for accurate detection") fmt.Println("Important headers to request:") for _, header := range wengine.ImportantHeaderNames { fmt.Printf(" - %s\n", header) } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.