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
+10.8k Golang : Web routing/multiplex example
+12.9k Golang : Convert(cast) int to int64
+13.2k Golang : Read XML elements data with xml.CharData example
+16.1k Golang : Check if a string contains multiple sub-strings in []string?
+8.1k Golang: Prevent over writing file with md5 hash
+20.2k Golang : Secure(TLS) connection between server and client
+14.3k Golang : Convert(cast) int to float example
+10.7k Golang : Replace a parameter's value inside a configuration file example
+10.2k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+5.8k Golang : Measure execution time for a function
+7.5k Golang : Trim everything onward after a word
+11.9k Golang : Split strings into command line arguments