0
点赞
收藏
分享

微信扫一扫

Golang:user_agent解析HTTP用户代理User Agents


UserAgent is a Go library that parses HTTP User Agents. As an example:

译文:UserAgent是解析HTTP用户代理的Go语言库

文档

  • ​​https://github.com/mssola/user_agent​​
  • ​​https://pkg.go.dev/github.com/mssola/user_agent​​

安装

go get github.com/mssola/user_agent

示例

package main

import (
"fmt"

"github.com/mssola/user_agent"
)

func main() {

ua := user_agent.New("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")

fmt.Printf("%v\n", ua.Mobile()) // => true
fmt.Printf("%v\n", ua.Bot()) // => false
fmt.Printf("%v\n", ua.Mozilla()) // => "5.0"

fmt.Printf("%v\n", ua.Platform()) // => "Linux"
fmt.Printf("%v\n", ua.OS()) // => "Android 2.3.7"

name, version := ua.Engine()
fmt.Printf("%v\n", name) // => "AppleWebKit"
fmt.Printf("%v\n", version) // => "533.1"

name, version = ua.Browser()
fmt.Printf("%v\n", name) // => "Android"
fmt.Printf("%v\n", version) // => "4.0"

// Let's see an example with a bot.
ua.Parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")

fmt.Printf("%v\n", ua.Bot()) // => true

name, version = ua.Browser()
fmt.Printf("%v\n", name) // => Googlebot
fmt.Printf("%v\n", version) // => 2.1
}

参考
​​​「Go工具箱」解析http中的user-agent,就用这个包:user_agent​​


举报

相关推荐

0 条评论