Home >Backend Development >Golang >Using uname to get the processor architecture in mac gives wrong results when building go binaries for amd64
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.
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?
Quote@hrdy
Comment 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!