Home  >  Article  >  Backend Development  >  Using uname to get the processor architecture in mac gives wrong results when building go binaries for amd64

Using uname to get the processor architecture in mac gives wrong results when building go binaries for amd64

王林
王林forward
2024-02-09 13:09:07515browse

当为 amd64 构建 go 二进制文件时,使用 uname 在 mac 中获取处理器架构会给出错误的结果

php editor Xigua discovered a problem when processing amd64 build go binary files. On Mac systems, using uname to obtain the processor architecture can lead to incorrect results. This issue may have an impact on the compilation and build process, so it needs to be noted. The correct way to obtain the processor architecture is through go's built-in function runtime.GOARCH. This can avoid errors caused by uname and ensure the accuracy of compilation and construction.

Question content

I have a golang service and need to obtain the architecture type of the processor

archCmd := exec.Command("uname", "-m")
arch, _ := archCmd.CombinedOutput()

I'm running the same binary on Intel and Arm machines. Binaries are built with

GOOS=darwin GOARCH=amd64

On ARM machines, this gets x86_64. This seems to be happening since I built with GOARCH=amd64 . But I'm not sure why this is happening. If I want to run the same build binary on intel and arm machines, what are my alternatives?

Workaround

Quote@hrdyComment describing this issue:

Quoting the separate superuser answer: https://www.php.cn/link/211fff9e65c0e47a790c629116e32996

...This will require some conditional logic to detect the operating system and parsing to detect the Apple CPU type. This may break in the future if this string value changes.

archCmd := exec.Command("sysctl", "machdep.cpu.brand_string")
arch, _ := archCmd.CombinedOutput()

Again, credit to @selalerercapitolis for the answer link.

The above is the detailed content of Using uname to get the processor architecture in mac gives wrong results when building go binaries for amd64. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete