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.4k Golang : Stop goroutine without channel
+19.1k Mac OSX : Homebrew and Golang
+23.7k Golang : Fix type interface{} has no field or no methods and type assertions example
+48k Golang : How to convert JSON string to map and slice
+7k Golang : A simple forex opportunities scanner
+7.2k Golang : How to iterate a slice without using for loop?
+15.1k Golang : How to check if IP address is in range
+16.3k Golang : How to implement two-factor authentication?
+4.9k Golang : Constant and variable names in native language
+20.2k Golang : Check if os.Stdin input data is piped or from terminal
+8.8k Golang : Find network service name from given port and protocol