runtime.GOOS
is your friend. However, keep in mind that you can't set constants based on it (although you can copy it to your own constant) - only variables, and only in runtime. You can use an init()
function in a module to run the detection automatically when the program starts.
package main
import "fmt"
import "runtime"
func main() {
fmt.Println("this is", runtime.GOOS)
foo := 1
switch runtime.GOOS {
case "linux":
foo = 2
case "darwin":
foo = 3
case "nacl": //this is what the playground shows!
foo = 4
default:
fmt.Println("What os is this?", runtime.GOOS)
}
fmt.Println(foo)
}