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.3k Golang : Convert a rune to unicode style string \u
+16.2k Golang : Connect to database (MySQL/MariaDB) server
+22.1k Golang : Validate email address with regular expression
+10.8k Golang : Convert Unix timestamp to UTC timestamp
+7k Golang : How to force compile or remove object files first before rebuild?
+3.3k Golang : Count leading or ending zeros(any item of interest) example
+6.1k Golang : Force download file example
+5.5k Golang : Find commonalities in two slices or arrays example
+6.6k Get form post value in Go
+3.7k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+2.3k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+16.9k Golang : Convert CSV data to JSON format and save to file