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.9k Linux : How to fix Brother HL-1110 printing blank page problem
+6.6k Golang : Implementing class(object-oriented programming style)
+22k Golang : Get current file path of a file or executable
+7.8k Android Studio : Checkbox for user to select options example
+14.7k Golang : Read data from config file and assign to variables
+8.9k Golang : How to flush a channel before the end of program?
+30.2k Golang : How to split or chunking a file to smaller pieces?
+3k Javascript : Access JSON data example
+11.7k Golang : Reset buffer example
+3.9k Golang : Reclaim memory occupied by make() example
+4.1k nginx : force all pages to be SSL
+26.3k Golang : bufio.NewReader.ReadLine to read file line by line