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
+5.8k CloudFlare : Another way to get visitor's real IP address
+20.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+5.6k Golang : Totalize or add-up an array or slice example
+12.6k Golang : How to determine if a year is leap year?
+17.2k Golang : Generate thumbnails from images
+27.5k Golang : Saving(serializing) and reading file with GOB
+5.6k Golang : Skip or discard items of non-interest when iterating example
+8.9k Golang : Channels and buffered channels examples
+7.3k Golang : How to check variable or object type during runtime?
+6.1k Golang : Validate credit card example
+12.7k Golang : Get current time
+6.1k Nginx : How to block user agent ?