2026/3/4 16:40:36
网站建设
项目流程
搜启网站建设,wordpress登录密码错误也不报错,泰安网络建设,成品短视频app有哪些在 Gin 框架中解析 User-Agent 判断设备类型#xff0c;可以通过 手动解析 或 第三方库 实现#xff0c;推荐结合 Gin 中间件 使用。以下是具体方案#xff1a;一、手动解析#xff08;零依赖#xff09;直接在 Gin 处理器中检查 User-Agent 字符串#xff0c;适合简单场…在 Gin 框架中解析 User-Agent 判断设备类型可以通过手动解析或第三方库实现推荐结合Gin 中间件使用。以下是具体方案一、手动解析零依赖直接在 Gin 处理器中检查 User-Agent 字符串适合简单场景go复制package main import ( net/http strings github.com/gin-gonic/gin ) // IsMobile 判断是否为移动设备 func IsMobile(userAgent string) bool { if userAgent { return false } ua : strings.ToLower(userAgent) mobileKeywords : []string{ mobile, android, iphone, ipad, ipod, windows phone, phone, pad, symbian, blackberry, nokia, samsung, htc, } for _, keyword : range mobileKeywords { if strings.Contains(ua, keyword) { return true } } return false } func main() { r : gin.Default() r.GET(/, func(c *gin.Context) { userAgent : c.GetHeader(User-Agent) deviceType : PC端 if IsMobile(userAgent) { deviceType 移动端 } c.JSON(http.StatusOK, gin.H{ user_agent: userAgent, device_type: deviceType, }) }) r.Run(:8080) }二、使用第三方库推荐1. 使用mileusna/useragent库安装bash复制go get github.com/mileusna/useragentGin 集成示例go复制package main import ( net/http github.com/gin-gonic/gin github.com/mileusna/useragent ) func main() { r : gin.Default() r.GET(/, func(c *gin.Context) { uaString : c.GetHeader(User-Agent) ua : useragent.Parse(uaString) c.JSON(http.StatusOK, gin.H{ user_agent: uaString, device: ua.Device, // PC / Phone / Tablet / Bot os: ua.OS, browser: ua.Name, version: ua.Version, mobile: ua.Device Phone || ua.Device Tablet, }) }) r.Run(:8080) }2. 使用mssola/useragent库安装bash复制go get github.com/mssola/useragentGin 集成示例go复制package main import ( net/http github.com/gin-gonic/gin github.com/mssola/useragent ) func main() { r : gin.Default() r.GET(/, func(c *gin.Context) { uaString : c.GetHeader(User-Agent) ua : useragent.New() ua.Parse(uaString) c.JSON(http.StatusOK, gin.H{ user_agent: uaString, mobile: ua.Mobile(), os: ua.OS(), browser: ua.Name(), version: ua.Version(), device_type: func() string { if ua.Mobile() { return 移动端 } return PC端 }(), }) }) r.Run(:8080) }三、最佳实践Gin 中间件模式将设备类型判断逻辑封装为中间件供全局或局部路由使用go复制package main import ( net/http strings github.com/gin-gonic/gin ) // DeviceType 设备类型常量 const ( DevicePC pc DeviceMobile mobile DeviceTablet tablet ) // DeviceInfo 设备信息 type DeviceInfo struct { Type string UserAgent string } const deviceInfoKey _device_info // DeviceMiddleware 设备检测中间件 func DeviceMiddleware() gin.HandlerFunc { return func(c *gin.Context) { userAgent : c.GetHeader(User-Agent) deviceType : detectDevice(userAgent) // 将设备信息存储到 Gin Context c.Set(deviceInfoKey, DeviceInfo{ Type: deviceType, UserAgent: userAgent, }) // 为模板渲染添加全局变量 c.Set(isMobile, deviceType DeviceMobile) c.Set(isPC, deviceType DevicePC) c.Next() } } // detectDevice 检测设备类型 func detectDevice(userAgent string) string { if userAgent { return DevicePC } ua : strings.ToLower(userAgent) // 平板识别 tabletKeywords : []string{ipad, android pad, tablet, kindle} for _, keyword : range tabletKeywords { if strings.Contains(ua, keyword) { return DeviceTablet } } // 手机识别 mobileKeywords : []string{ mobile, android, iphone, ipod, windows phone, phone, symbian, blackberry, nokia, htc, } for _, keyword : range mobileKeywords { if strings.Contains(ua, keyword) { return DeviceMobile } } return DevicePC } // GetDeviceInfo 从Context获取设备信息 func GetDeviceInfo(c *gin.Context) (DeviceInfo, bool) { info, exists : c.Get(deviceInfoKey) if !exists { return DeviceInfo{}, false } return info.(DeviceInfo), true } func main() { r : gin.Default() // 应用全局中间件 r.Use(DeviceMiddleware()) // 路由示例 r.GET(/, func(c *gin.Context) { deviceInfo, _ : GetDeviceInfo(c) c.JSON(http.StatusOK, gin.H{ device_type: deviceInfo.Type, user_agent: deviceInfo.UserAgent, message: func() string { switch deviceInfo.Type { case DeviceMobile: return 您正在使用移动设备访问 case DeviceTablet: return 您正在使用平板设备访问 default: return 您正在使用PC访问 } }(), }) }) // 为移动端和PC端返回不同内容 r.GET(/home, func(c *gin.Context) { deviceInfo, _ : GetDeviceInfo(c) if deviceInfo.Type DeviceMobile { c.String(http.StatusOK, 移动端首页) return } c.String(http.StatusOK, PC端首页) }) r.Run(:8080) }四、方案对比表格复制方案优点缺点推荐场景手动解析无依赖、性能最好准确率一般、需维护关键词简单项目、对性能要求极高第三方库准确率高、功能完整增加依赖、性能略低复杂项目、需要详细设备信息中间件模式代码复用、逻辑清晰轻微性能损耗所有 Gin 项目最佳实践生产环境建议使用第三方库 中间件组合平衡准确性与开发效率。