Go语言是否依赖于CPU?

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

Is Go language CPU dependent?

问题

Go语言是否依赖于CPU?

我知道它支持x86、x86_64和ARM。它是否有一些CPU相关的代码,比如汇编代码块?

PS 我没有表达清楚。Go语言的实现是否依赖于CPU?

我不希望在我的程序中添加ARM汇编代码。我想知道是否可以将Go程序仅编译为x86(_64)和ARM,而不支持其他平台。

英文:

Is Go language CPU dependent?

I know it supports x86, x86_64 and ARM. Does it have some CPU depend code like assembler code blocks?

PS I was not clear enough. Does Go language implementation is CPU dependent?

I do not wish to add ARM assembly code in my program. I am wondering if Go program could be compiled on x86(_64) and ARM only and all other platforms are not supported.

答案1

得分: 12

Go语言是编译型语言,因此最终结果确实是(特定于CPU的)机器码。

$ echo 'package main\nfunc main(){ println("hello world") }' > hello.go
$ go build hello.go
$ objdump -D hello | head

hello: 文件格式 elf32-i386

.text的反汇编:

08048c00 <main.main>:
8048c00: 65 8b 0d 00 00 00 00 mov %gs:0x0,%ecx
8048c07: 8b 49 f8 mov -0x8(%ecx),%ecx
8048c0a: 3b 21 cmp (%ecx),%esp

因此,你不能只是拿一个编译为ARM的可执行文件,在x86上运行它。

尽管如此,Go语言对于跨平台编译程序有着出色的支持,所以在大多数情况下,你不需要一堆运行不同操作系统的机器(虚拟或实际)来为这些目标编译你的程序。

英文:

Go is compiled, so the end result is indeed (CPU-specific) machine code.

$ echo &#39;package main\nfunc main(){ println(&quot;hello world&quot;) }&#39; &gt; hello.go
$ go build hello.go
$ objdump -D hello | head

hello:     file format elf32-i386


Disassembly of section .text:

08048c00 &lt;main.main&gt;:
 8048c00:	65 8b 0d 00 00 00 00 	mov    %gs:0x0,%ecx
 8048c07:	8b 49 f8             	mov    -0x8(%ecx),%ecx
 8048c0a:	3b 21                	cmp    (%ecx),%esp

So you won't be able to just take executable compiled for, say, ARM, and run it on x86.

Despite that, Go has excellent support for cross-compiling programs for different OSes and architectures, so in most cases you won't need a bunch of machines (virtual or real) running different OSes to compile your programs for those targets.

huangapple
  • 本文由 发表于 2014年1月3日 20:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/20904062.html
匿名

发表评论

匿名网友

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

确定