How do I determine whether the program was built as 32 bit or 64 bit in Go?

huangapple go评论97阅读模式
英文:

How do I determine whether the program was built as 32 bit or 64 bit in Go?

问题

我能找到的最接近的是runtime.GOARCH,但它可能会返回arm,而arm既可以是32位也可以是64位。


我只关心这个程序是如何构建的,而不关心操作系统是否支持64位可执行文件。例如,在AArch64 CPU上的ARM模式或x86-64 CPU上的32位兼容模式下,我仍然希望得到32位,因为这是程序运行的模式。

相关链接:https://stackoverflow.com/questions/66254265/detect-os-x86-or-x64-when-compiled-as-x86-in-go 是关于检测操作系统支持的内容,例如可能运行不同编译的可执行文件。

英文:

The closest I can get is runtime.GOARCH, but that might also give arm, which could be either 32 or 64 bit.


I only care how this program was built, not whether the OS also supports 64-bit executables. e.g. for ARM mode on an AArch64 CPU or 32-bit compat mode on an x86-64 CPU, I still want 32 because that's the mode this program is running in.

Related: https://stackoverflow.com/questions/66254265/detect-os-x86-or-x64-when-compiled-as-x86-in-go is about detecting what the OS supports, e.g. for maybe running a differently-compiled executable.

答案1

得分: 6

使用GOARCH来指定arm架构: arm (ARM) 和 arm64 (AArch64)。

> 可选的环境变量
>
> $GOOS 和 $GOARCH
>
> 目标操作系统和编译架构的名称。
> 这些默认值分别为 $GOHOSTOS 和 $GOHOSTARCH 的值
> (下面有描述)。
>
> $GOOS 的选择有
>
> $GOOS $GOARCH
> darwin 386
> darwin amd64
> darwin arm
> darwin arm64
> dragonfly amd64
> freebsd 386
> freebsd amd64
> freebsd arm
> linux 386
> linux amd64
> linux arm
> linux arm64
> linux ppc64
> linux ppc64le
> linux mips64
> linux mips64le
> netbsd 386
> netbsd amd64
> netbsd arm
> openbsd 386
> openbsd amd64
> openbsd arm
> plan9 386
> plan9 amd64
> solaris amd64
> windows 386
> windows amd64

英文:

Use GOARCH for arm: arm (ARM) and arm64 (AArch64),

> Optional environment variables
>
> $GOOS and $GOARCH
>
> The name of the target operating system and compilation architecture.
> These default to the values of $GOHOSTOS and $GOHOSTARCH respectively
> (described below).
>
> Choices for $GOOS are
>
> $GOOS $GOARCH
> darwin 386
> darwin amd64
> darwin arm
> darwin arm64
> dragonfly amd64
> freebsd 386
> freebsd amd64
> freebsd arm
> linux 386
> linux amd64
> linux arm
> linux arm64
> linux ppc64
> linux ppc64le
> linux mips64
> linux mips64le
> netbsd 386
> netbsd amd64
> netbsd arm
> openbsd 386
> openbsd amd64
> openbsd arm
> plan9 386
> plan9 amd64
> solaris amd64
> windows 386
> windows amd64

答案2

得分: 3

const is64Bit = uint64(^uintptr(0)) == ^uint64(0)

这段代码的作用是判断当前系统是否为64位。如果 uintptr 是32位,^uintptr(0) 的结果将是 0xffffffff 而不是 0xffffffffffffffff

无论是32位还是64位架构,^uint64(0) 的结果始终是 0xffffffffffffffff

英文:
const is64Bit = uint64(^uintptr(0)) == ^uint64(0)

This works because if uintptr is 32 bits, ^uintptr(0) will be 0xffffffff rather than 0xffffffffffffffff.

^uint64(0) will always be 0xffffffffffffffff regardless of 32 or 64 bit architectures.

huangapple
  • 本文由 发表于 2016年3月16日 08:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/36024542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定