go 的编译和运行环境
最编程
2024-04-22 20:22:16
...
开篇
很多语言都有自己的运行时环境,go自然也不例外,那么今天我们就来讲讲go语言的运行时环境!
不同语言的运行时环境对比
我们都知道Java的运行时环境是jvm ,javascript的运行时环境是浏览器内核
Java -->jvm
javascript-->浏览器内核
他们一般的运行流程可以归纳为:code-->bytecode-->runtime-->os
那么go的运行时环境是什么样呢?
go没有虚拟机的概念,go的runtime是代码,Runtime作为程序的一部分打包进二进制产物,Runtime随用户程序一起运行,Runtime与用户程序没有明显界限,直接通过函数调用!
go的runtime的能力有哪些呢?
- 内存管理能力
- 垃圾回收能力
- 超强的并发能力
- Runtime有一定的屏蔽系统调用能力
比如:
一些go的关键字其实是Runtime下的函数:
关键字 函数
go newproc
new newobject
make makeslice, makechain, makemap...
<- chansend1, chanrecv1
这样就屏蔽了系统调用!
如何编译go的代码
进入目标路径输入go build -n
即可
输出信息如下:
#
# _/D_/GolandProjects/goDemo/main
#
mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg << 'EOF' # internal
# import config
packagefile fmt=D:\gosdk\go1.16.5\pkg\windows_amd64\fmt.a
packagefile runtime=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime.a --- 说明编译的时候自带运行环境
EOF
cd D:\GolandProjects\goDemo\main
"D:\\gosdk\\go1.16.5\\pkg\\tool\\windows_amd64\\compile.exe" -o "$WORK\\b001\\_pkg_.a" -trimpath "$WORK\\b001=>" -p main -complete -buildid rQb4_uWwlL9RdRhAPVJd/rQb4_uWwlL9RdRhAPVJd -go
version go1.16.5 -D _/D_/GolandProjects/goDemo/main -importcfg "$WORK\\b001\\importcfg" -pack -c=4 "D:\\GolandProjects\\goDemo\\main\\interface.go"
"D:\\gosdk\\go1.16.5\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\_pkg_.a" # internal
cat >$WORK\b001\importcfg.link << 'EOF' # internal
packagefile _/D_/GolandProjects/goDemo/main=$WORK\b001\_pkg_.a
packagefile fmt=D:\gosdk\go1.16.5\pkg\windows_amd64\fmt.a
packagefile runtime=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime.a
packagefile errors=D:\gosdk\go1.16.5\pkg\windows_amd64\errors.a
packagefile internal/fmtsort=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\fmtsort.a
packagefile io=D:\gosdk\go1.16.5\pkg\windows_amd64\io.a
packagefile math=D:\gosdk\go1.16.5\pkg\windows_amd64\math.a
packagefile os=D:\gosdk\go1.16.5\pkg\windows_amd64\os.a
packagefile reflect=D:\gosdk\go1.16.5\pkg\windows_amd64\reflect.a
packagefile strconv=D:\gosdk\go1.16.5\pkg\windows_amd64\strconv.a
packagefile sync=D:\gosdk\go1.16.5\pkg\windows_amd64\sync.a
packagefile unicode/utf8=D:\gosdk\go1.16.5\pkg\windows_amd64\unicode\utf8.a
packagefile internal/bytealg=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\bytealg.a
packagefile internal/cpu=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\cpu.a
packagefile runtime/internal/atomic=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime\internal\atomic.a
packagefile runtime/internal/math=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime\internal\math.a
packagefile runtime/internal/sys=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime\internal\sys.a
packagefile internal/reflectlite=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\reflectlite.a
packagefile sort=D:\gosdk\go1.16.5\pkg\windows_amd64\sort.a
packagefile math/bits=D:\gosdk\go1.16.5\pkg\windows_amd64\math\bits.a
packagefile internal/oserror=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\oserror.a
packagefile internal/poll=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\poll.a
packagefile internal/syscall/execenv=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\syscall\execenv.a
packagefile internal/syscall/windows=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\syscall\windows.a
packagefile internal/testlog=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\testlog.a
packagefile io/fs=D:\gosdk\go1.16.5\pkg\windows_amd64\io\fs.a
packagefile sync/atomic=D:\gosdk\go1.16.5\pkg\windows_amd64\sync\atomic.a
packagefile syscall=D:\gosdk\go1.16.5\pkg\windows_amd64\syscall.a
packagefile time=D:\gosdk\go1.16.5\pkg\windows_amd64\time.a
packagefile unicode/utf16=D:\gosdk\go1.16.5\pkg\windows_amd64\unicode\utf16.a
packagefile internal/unsafeheader=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\unsafeheader.a
packagefile unicode=D:\gosdk\go1.16.5\pkg\windows_amd64\unicode.a
packagefile internal/race=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\race.a
packagefile internal/syscall/windows/sysdll=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\syscall\windows\sysdll.a
packagefile path=D:\gosdk\go1.16.5\pkg\windows_amd64\path.a
packagefile internal/syscall/windows/registry=D:\gosdk\go1.16.5\pkg\windows_amd64\internal\syscall\windows\registry.a
EOF
mkdir -p $WORK\b001\exe\
cd .
"D:\\gosdk\\go1.16.5\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\a.out.exe" -importcfg "$WORK\\b001\\importcfg.link" -buildmode=pie -buildid=bMBX0ZdidGJ5isGv8iHu/rQb4_uWw
lL9RdRhAPVJd/rQb4_uWwlL9RdRhAPVJd/bMBX0ZdidGJ5isGv8iHu -extld=gcc "$WORK\\b001\\_pkg_.a"
"D:\\gosdk\\go1.16.5\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\exe\\a.out.exe" # internal
mv $WORK\b001\exe\a.out.exe main.exe
我们可以看到这样一行输出信息:packagefile runtime=D:\gosdk\go1.16.5\pkg\windows_amd64\runtime.a
说明编译的时候自带运行环境
go语言的编译过程
词法分析--->句法分析--->语义分析--->中间码生成-->代码优化--->机器码生成--->链接
注意????:编译过程都干了些啥事可以去看看编译原理,中间码是平台无关的,机器码是平台相关的!
推荐阅读
-
来源:第 39 期]Go 和 Scala 成为最赚钱的语言;400 名外包公司高管接受收购协议
-
使用 apt-get 安装 telnet、curl、ifconfig、vim、ping 和其他工具的 Linux 环境 [翻译
-
用于 CanMV K230 的 CanMV-IDE 环境和 MicroPython 编程,作者:KANNAN KANZHI
-
Linux]Linux 环境基本工具(yum、vim)的基本使用和配置(继续)
-
.NET框架设计和实施说明6.5 - ASP.NET Core应用程序的多种运行模式V - Kestrel的其他说明
-
如何修改 Orientop 的 java 运行环境 Orientop 设置内存
-
Hadoop 运行模式 (II)、SSH 未加密登录配置、生成公钥和私钥、群集配置、群集部署规划、默认配置文件、核心配置文件、HDFS 配置文件、YARN 配置文件、MapReduce 配置文件、分发配置的
-
案例:分布式 LAMP 架构构建 Discuz 论坛(编译和安装) - Mysql 的编译和安装(192.168.10.4 主机)
-
UNIX 之父肯和丹尼斯(第二部分)--也许是因为宣传的缘故,今天人们的注意力大多集中在 "野生 "黑客身上,更多关注的是他们造成的破坏,而不是他们给技术带来的突破。如果回到 50 年前,情况就完全不同了。那时的黑客更像是为了爱好而自愿加班的模范员工,他们根本不在自己家里工作。 当然,那时的电脑还远远买不起。如果你对计算机技术感兴趣,就必须投身于学术机构或巨型企业。比如通用电气或贝尔实验室。 肯尼斯-莱恩-汤普森(Kenneth Lane Thompson)就是这些老派黑客中的一员,黑客们亲切地称他为 "肯"。他出生于 1943 年的前婴儿潮时代,22 岁从大学毕业,一年后获得硕士学位--这两个学位都来自加州大学伯克利分校的电子工程和计算科学专业。随后,他进入贝尔实验室,开始了 Multics 的研发工作。 不过,他并不总是在工作。他利用大型计算机编写了一款名为 "星际迷航 "的游戏,他和同事丹尼斯-里奇(Dennis Ritchie)在办公室里玩这款游戏。因此,当贝尔实验室在 1969 年退出 Multics 计划时,他和丹尼斯都有点失望。 不过很快,他们就找到了一台闲置的 PDP-7 机器。这台机器在当时属于低端产品,售价只有 7.2 万美元,所以贝尔实验室并没有太在意。幸运的是肯重写了《星际迷航》程序,开发了基于 Multics 的新操作系统,以便在 PDP-7 上运行游戏,甚至还为操作系统开发了一种新的编程语言 "B"。 这位 Unix 之父没有得到应有的尊重。
-
编号 - 个人对蓝鲸运行和维护的理解