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
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+12.6k Golang : unknown escape sequence error
+12.9k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+5.4k Golang : Test input string for unicode example
+12.4k Golang : Get dimension(width and height) of image file
+5.3k Golang : How to solve "too many .rsrc sections" error?
+29.7k error: trying to remove "yum", which is protected
+25.9k Golang : Convert CSV data to JSON format and save to file
+6k Golang : How to call function inside template with template.FuncMap
+6.9k Golang : Regular Expression find string example
+18.3k Golang : How to Set or Add Header http.ResponseWriter?
+29.4k Golang : Download file example