Golang : How to determine if user agent is a mobile device example
With the increasing number of people accessing the Internet with mobile devices such as tablet or smart phone apart from desktop. The ability to detect which devices that your visitor is using to visit your web site/app is crucial. Once you're able to detect the correct device, you can then customize the UX or other feature specifically for the devices.
For this tutorial, we will create a function to determine if a user agent is a mobile device (iPad, Android, etc) or not.
Here you go!
package main
import (
"fmt"
"net/http"
"strings"
)
func is_mobile(useragent string) bool {
// the list below is taken from
// https://github.com/bcit-ci/CodeIgniter/blob/develop/system/libraries/User_agent.php
mobiles := []string{"Mobile Explorer","Palm","Motorola","Nokia","Palm","Apple iPhone","iPad","Apple iPod Touch","Sony Ericsson","Sony Ericsson","BlackBerry","O2 Cocoon","Treo", "LG", "Amoi", "XDA", "MDA", "Vario", "HTC", "Samsung",
"Sharp", "Siemens", "Alcatel", "BenQ", "HP iPaq", "Motorola", "PlayStation Portable", "PlayStation 3", "PlayStation Vita", "Danger Hiptop", "NEC", "Panasonic", "Philips", "Sagem", "Sanyo", "SPV", "ZTE", "Sendo", "Nintendo DSi", "Nintendo
DS", "Nintendo 3DS", "Nintendo Wii", "Open Web", "OpenWeb", "Android", "Symbian", "SymbianOS", "Palm", "Symbian S60", "Windows CE", "Obigo", "Netfront Browser", "Openwave Browser", "Mobile Explorer", "Opera Mini", "Opera Mobile", "Firefox
Mobile", "Digital Paths", "AvantGo", "Xiino", "Novarra Transcoder", "Vodafone", "NTT DoCoMo", "O2", "mobile", "wireless", "j2me", "midp", "cldc", "up.link", "up.browser", "smartphone", "cellphone", "Generic Mobile"}
for _, device := range mobiles {
if strings.Index(useragent, device) > -1 {
return true
}
}
return false
}
func checkIfUserAgentIsAMobile(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
fmt.Printf("user agent is: %s \n", ua)
w.Write([]byte("user agent is " + ua + "\n"))
result := "no"
if is_mobile(ua) {
result = "yes"
}
fmt.Printf("user agent is a mobile: %v \n", is_mobile(ua))
w.Write([]byte("user agent is a mobile:" + result + "\n"))
}
func main() {
http.HandleFunc("/", checkIfUserAgentIsAMobile)
http.ListenAndServe(":8080", nil)
}
Sample output:
from a visit by iPad:
user agent is Mozilla/5.0 (iPad; CPU OS 921 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.107 Mobile/13D15 Safari/601.1.46
user agent is a mobile:yes
from a Mac:
user agent is Mozilla/5.0 (Macintosh; Intel Mac OS X 1085) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
user agent is a mobile:no
References:
https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/user_agents.php
https://socketloop.com/tutorials/golang-check-if-user-agent-is-a-robot-or-crawler-example
See also : Golang : Check if user agent is a robot or crawler example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+52.1k Golang : How to get struct field and value by name
+5.6k Javascript : How to replace HTML inside <div>?
+11.4k Golang : Fuzzy string search or approximate string matching example
+11.1k Golang : Characters limiter example
+8.6k Golang : Random integer with rand.Seed() within a given range
+9.8k Golang : Channels and buffered channels examples
+7.7k Golang : Grayscale Image
+9.8k CodeIgniter : Load different view for mobile devices
+5.8k Golang : Extract unicode string from another unicode string example
+40.8k Golang : How to count duplicate items in slice/array?
+17k Golang : When to use init() function?
+9.2k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases