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
+35.4k Golang : Integer is between a range
+16.2k Golang : Find out mime type from bytes in buffer
+21.6k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+8.1k Swift : Convert (cast) Character to Integer?
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+16.8k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+13.6k Golang : How to determine if a year is leap year?
+9.2k Golang : Apply Histogram Equalization to color images
+20.7k Golang : Underscore or snake_case to camel case example
+10.8k Nginx : TLS 1.2 support
+18.9k Golang : Check whether a network interface is up on your machine