| 1 | # Golang User Agent parser |
| 2 | |
| 3 | This is the Go implementation of the [ua-parser](https://github.com/ua-parser) |
| 4 | |
| 5 | # Usage |
| 6 | |
| 7 | ## Install |
| 8 | |
| 9 | $ go get github.com/ua-parser/uap-go/uaparser |
| 10 | |
| 11 | ## Updating |
| 12 | |
| 13 | `uap-core` definitions are, by default, compiled and included in the Go portion of this package. To regenerate these definitions you can run the `build.sh` script. |
| 14 | |
| 15 | ## Testing |
| 16 | |
| 17 | $ cd uaparser |
| 18 | $ go test -v -cover |
| 19 | |
| 20 | When adding new feature you can check for data race with `go test -race`, although this command |
| 21 | will take more than 2 minutes to execute since the `-race` flag adds some instrumentation on all regex matches |
| 22 | |
| 23 | So a quicker way to test data race on the main `*parser.Parse` method can be: |
| 24 | |
| 25 | $ go test -race -run=Concurrency # filter to execute `TestGenericParseMethodConcurrency` only |
| 26 | |
| 27 | ## Benching |
| 28 | |
| 29 | If needed, you can run benchmark on your latest feature to be compared (using `benchcmp`) against the current performance |
| 30 | |
| 31 | $ cd uaparser |
| 32 | $ git checkout master |
| 33 | $ go test -bench=. -run=none > ~/old.benchmark |
| 34 | $ git checkout my_latest_feature |
| 35 | $ go test -bench=. -run=none > ~/new.benchmark |
| 36 | $ benchcmp ~/old.benchmark ~/new.benchmark |
| 37 | |
| 38 | # Example |
| 39 | |
| 40 | ```go |
| 41 | package main |
| 42 | |
| 43 | import ( |
| 44 | "fmt" |
| 45 | "log" |
| 46 | |
| 47 | "github.com/ua-parser/uap-go/uaparser" |
| 48 | ) |
| 49 | |
| 50 | func main() { |
| 51 | uagent := "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true" |
| 52 | |
| 53 | parser, err := uaparser.New("./regexes.yaml") |
| 54 | if err != nil { |
| 55 | log.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | client := parser.Parse(uagent) |
| 59 | |
| 60 | fmt.Println(client.UserAgent.Family) // "Amazon Silk" |
| 61 | fmt.Println(client.UserAgent.Major) // "1" |
| 62 | fmt.Println(client.UserAgent.Minor) // "1" |
| 63 | fmt.Println(client.UserAgent.Patch) // "0-80" |
| 64 | fmt.Println(client.Os.Family) // "Android" |
| 65 | fmt.Println(client.Os.Major) // "" |
| 66 | fmt.Println(client.Os.Minor) // "" |
| 67 | fmt.Println(client.Os.Patch) // "" |
| 68 | fmt.Println(client.Os.PatchMinor) // "" |
| 69 | fmt.Println(client.Device.Family) // "Kindle Fire" |
| 70 | } |
| 71 | |
| 72 | ``` |
| 73 | |
| 74 | # Authors |
| 75 | |
| 76 | * Yihuan Zhou |
| 77 | |
| 78 | (Based on the Java implementation by Steve Jiang and using agent data from BrowserScope) |