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

王林
Release: 2024-02-09 13:09:07
forward
515 people have browsed it

当为 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()
Copy after login

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

GOOS=darwin GOARCH=amd64
Copy after login

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()
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!