go的编译以及运行时环境

开篇

很多语言都有自己的运行时环境,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 复制代码
关键字     函数
go       newproc
new      newobject
make     makeslice, makechain, makemap...
<-       chansend1, chanrecv1

这样就屏蔽了系统调用!

如何编译go的代码

进入目标路径输入go build -n即可

输出信息如下:

bash 复制代码
#
# _/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语言的编译过程

词法分析--->句法分析--->语义分析--->中间码生成-->代码优化--->机器码生成--->链接

注意💡:编译过程都干了些啥事可以去看看编译原理,中间码是平台无关的,机器码是平台相关的!

相关推荐
你在我身后24 分钟前
Spring-JAVA
java·后端·spring
多敲代码防脱发5 小时前
Spring框架基本使用(Maven详解)
java·网络·后端·spring·maven
Asthenia04125 小时前
Mybatis实践——Wrapper&&三表联查&&BaseMapper和Service的功能分异
后端
B站计算机毕业设计超人6 小时前
计算机毕业设计SpringBoot+Vue.jst0甘肃非物质文化网站(源码+LW文档+PPT+讲解)
java·vue.js·spring boot·后端·spring·intellij-idea·课程设计
why技术6 小时前
可以说是一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
后端·面试
m0_748254666 小时前
定时任务特辑 Quartz、xxl-job、elastic-job、Cron四个定时任务框架对比,和Spring Boot集成实战
java·spring boot·后端
diemeng11197 小时前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust
Warren987 小时前
Springboot中分析SQL性能的两种方式
java·spring boot·后端·sql·mysql·intellij-idea
canonical_entropy7 小时前
Nop平台与橙单OrangeForm集成
后端·低代码
计算机学姐8 小时前
基于SpringBoot的校园消费点评管理系统
java·vue.js·spring boot·后端·mysql·spring·java-ee