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
+4.6k HTTP common errors and their meaning explained
+21.7k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+10.8k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+14k Golang : Recombine chunked files example
+22.4k Golang : Round float to precision example
+9.6k Golang : Function wrapper that takes arguments and return result example
+12.5k Golang : Convert int(year) to time.Time type
+18.3k Golang : Display list of time zones with GMT
+30.4k Golang : Download file example
+6.7k Golang : Calculate BMI and risk category
+5.5k Golang : Error handling methods
+13.9k Elastic Search : Mapping date format and sort by date