Golang : Detect (OS) Operating System
Ability to detect which type of operating system during run time can be helpful in programming a software behavior or prompt different output message to the user.
Go has build in function to detect the operating system and able to tell you the type of operating system is Windows or Unix/Linux during run time.
To detect the operating system, use the runtime.GOOS
For example :
Detect Windows
if runtime.GOOS == 'windows' {
fmt.Println('Windows OS detected')
}
Detect Linux
if runtime.GOOS == 'linux' { // also can be specified to FreeBSD
fmt.Println('Unix/Linux type OS detected')
}
Detect Mac OS/X
if runtime.GOOS == 'darwin' {
fmt.Println('Mac OS detected')
}
references:
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
+16.1k Golang : Find smallest number in array
+4.3k Gogland : Datasource explorer
+6.2k Golang : Use modern ciphers only in secure connection
+6.3k Golang : Word limiter example
+12.5k Golang : Tutorial on loading GOB and PEM files
+6.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+5.8k Mac/Linux/Windows : Get CPU information from command line
+42.6k Golang : Use wildcard patterns with filepath.Glob() example
+3.8k Linux : How to set root password in Linux Mint
+5.8k Fix sudo yum hang problem with no output or error messages
+15k Golang : Execute terminal command to remote machine example