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
+4.7k Golang : Calculate a pip value and distance to target profit example
+15.3k Golang : Get digits from integer before and after given position example
+11.3k CodeIgniter : Import Linkedin data
+5.6k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+8.8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+8.9k Golang : How to check if a string with spaces in between is numeric?
+5.9k Golang : Process non-XML/JSON formatted ASCII text file example
+9.4k Golang : Qt get screen resolution and display on center example
+9.5k Golang : Turn string or text file into slice example
+20.7k Golang : For loop continue,break and range
+13.7k Golang : concatenate(combine) strings
+5.8k PageSpeed : Clear or flush cache on web server