英文:
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 'package main\nfunc main(){ println("hello world") }' > hello.go
$ go build hello.go
$ objdump -D hello | head
hello: file format elf32-i386
Disassembly of section .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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论