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
+5.9k Golang : Debug with Godebug
+13.8k Golang : Fix image: unknown format error
+14.6k Golang : How to check for empty array string or string?
+34.7k Golang : Upload and download file to/from AWS S3
+19.6k Golang : Count JSON objects and convert to slice/array
+12.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+7.9k Golang : HttpRouter multiplexer routing example
+30.2k Golang : Remove characters from string example
+7.9k Golang : Append and add item in slice
+5.4k Golang : Frobnicate or tweaking a string example
+18.2k Golang : Get download file size
+6.5k Golang : Reverse by word