How can I check if my program is compiled for 32 or 64 bit processor?

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

How can I check if my program is compiled for 32 or 64 bit processor?

问题

有没有标准的方法来检查操作系统是32位还是64位?
我已经检查了runtime和os包,但没有找到。
http://play.golang.org/p/d6NywMDMcY

package main

import "fmt"
import "runtime"

func main() {
    fmt.Println(runtime.GOOS, runtime.GOARCH)
}
英文:

Is there any standard method to check os is 32 or 64 bit?
I've check runtime & os package, but can not found.
http://play.golang.org/p/d6NywMDMcY

package main

import "fmt"
import "runtime"

func main() {
	fmt.Println(runtime.GOOS, runtime.GOARCH)
}

答案1

得分: 14

32位或64位操作系统是什么意思?例如,GOARCH=amd64p32用于GOOS=nacl,表示使用64位指令,32位指针和32位类型intuint

package main

import (
	"fmt"
	"runtime"
	"strconv"
)

func main() {
	const PtrSize = 32 << uintptr(^uintptr(0)>>63)
	fmt.Println(runtime.GOOS, runtime.GOARCH)
	fmt.Println(strconv.IntSize, PtrSize)
}

输出:

nacl amd64p32
32 32

和

linux amd64
64 64
英文:

What do you mean by a 32- or 64-bit OS? For example, GOARCH=amd64p32, which is used for GOOS=nacl, is amd64 64-bit instructions with 32-bit pointers and 32-bit type ints and uints.

package main

import (
	&quot;fmt&quot;
	&quot;runtime&quot;
	&quot;strconv&quot;
)

func main() {
	const PtrSize = 32 &lt;&lt; uintptr(^uintptr(0)&gt;&gt;63)
	fmt.Println(runtime.GOOS, runtime.GOARCH)
	fmt.Println(strconv.IntSize, PtrSize)
}

Playground: http://play.golang.org/p/TKnCA0gqsI

Output:

nacl amd64p32
32 32

and

linux amd64
64 64

答案2

得分: -1

你可以使用unsafe包来计算指针的大小(32位系统为4字节,64位系统为8字节)。
这里有一个可运行的示例:http://play.golang.org/p/MPap9KMD1y

英文:

You can use the unsafe package and compute the size of a pointer (4 for 32bits, 8 for 64bits).
Here a working example: http://play.golang.org/p/MPap9KMD1y

huangapple
  • 本文由 发表于 2014年9月9日 18:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/25741841.html
匿名

发表评论

匿名网友

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

确定