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

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

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

  1. package main
  2. import "fmt"
  3. import "runtime"
  4. func main() {
  5. fmt.Println(runtime.GOOS, runtime.GOARCH)
  6. }
英文:

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

  1. package main
  2. import "fmt"
  3. import "runtime"
  4. func main() {
  5. fmt.Println(runtime.GOOS, runtime.GOARCH)
  6. }

答案1

得分: 14

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

  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strconv"
  6. )
  7. func main() {
  8. const PtrSize = 32 << uintptr(^uintptr(0)>>63)
  9. fmt.Println(runtime.GOOS, runtime.GOARCH)
  10. fmt.Println(strconv.IntSize, PtrSize)
  11. }

输出:

  1. nacl amd64p32
  2. 32 32
  3. linux amd64
  4. 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.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;runtime&quot;
  5. &quot;strconv&quot;
  6. )
  7. func main() {
  8. const PtrSize = 32 &lt;&lt; uintptr(^uintptr(0)&gt;&gt;63)
  9. fmt.Println(runtime.GOOS, runtime.GOARCH)
  10. fmt.Println(strconv.IntSize, PtrSize)
  11. }

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

Output:

  1. nacl amd64p32
  2. 32 32

and

  1. linux amd64
  2. 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:

确定