Skip to content

Instantly share code, notes, and snippets.

@clarkmcc
Created March 5, 2020 17:58
Show Gist options
  • Save clarkmcc/d864f903e2e7edb385d8db77cf6180f8 to your computer and use it in GitHub Desktop.
Save clarkmcc/d864f903e2e7edb385d8db77cf6180f8 to your computer and use it in GitHub Desktop.
Golang function to determine whether a MacOS machine is 32 bit or 64 bit
func GetArch() (string, *errors.ErrorCode) {
cmd := exec.Command(
"getconf",
"LONG_BIT",
)
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error getting arch: %v", err)
return "", errors.New(errors.UnsupportedArchitecture)
}
is64 := strings.Index(string(output), "64")
is32 := strings.Index(string(output), "32")
if is32 < 0 && is64 < 0 {
return "", errors.New(errors.UnsupportedArchitecture)
}
if is32 >= 0 {
return "32", nil
} else if is64 >= 0 {
return "64", nil
}
return "", errors.New(errors.UnsupportedArchitecture)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment