Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ShortenUA ¶
ShortenUA shortens a User-Agent string by replacing common strings with small tokens.
Use UnshortenUA() to reverse it.
Example:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 ~Z (~W NT 10.0; Win64; x64) ~a537.36 ~G ~c80.0.3987.132 ~s537.36
The goal is not to produce the shortest output, but to provide a reasonably short output while maintaining readability.
Inspired by: https://github.com/icza/gox/blob/master/netx/httpx/httpx.go
Types ¶
type UserAgent ¶
func ParseUA ¶
ParseUA parses a User-Agent header.
Example ¶
package main import ( "fmt" "zgo.at/gadget" ) func main() { ua := gadget.ParseUA(`Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0`) fmt.Println(ua.String()) // "Firefox 73 on Windows 10" fmt.Println(ua.Browser()) // "Firefox 73" fmt.Println(ua.OS()) // "Windows 10" // Or for more detailed information: fmt.Println(ua.BrowserName) // "Firefox" fmt.Println(ua.BrowserVersion) // "73" fmt.Println(ua.OSName) // "Windows" fmt.Println(ua.OSVersion) // "10" // Helper to shorten the UA string while remaining readable: uaHeader := `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4029.0 Safari/537.36` short := gadget.ShortenUA(uaHeader) fmt.Println(short) // ~Z (~W NT 10.0; Win64; x64) ~a537.36 ~G ~c81.0.4029.0 ~s537.36 fmt.Println(gadget.UnshortenUA(short) == uaHeader) // true }
Output: Firefox 73 on Windows 10 Firefox 73 Windows 10 Firefox 73 Windows 10 ~Z (~W NT 10.0; Win64; x64) ~a537.36 ~G ~c81.0.4029.0 ~s537.36 true
Click to show internal directories.
Click to hide internal directories.