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
+23.3k Golang : Print out struct values in string format
+30.1k Golang : Get and Set User-Agent examples
+5.9k Golang : Struct field tags and what is their purpose?
+6.6k Golang : How to search a list of records or data structures
+9.5k Golang : How to get ECDSA curve and parameters data?
+9.2k Golang : Go as a script or running go with shebang/hashbang style
+8.8k Golang : Another camera capture GUI application with GTK and OpenCV
+7.3k Golang : Gargish-English language translator
+8.8k Golang : Combine slices but preserve order example
+5.9k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+10.1k Golang : Turn string or text file into slice example
+21.1k Golang : Underscore or snake_case to camel case example