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语言的编译过程

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

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

相关推荐
步行cgn5 分钟前
Spring 全注解开发详解
java·后端·spring
IT_陈寒3 小时前
SpringBoot启动慢得像蜗牛?原来是这个配置在捣鬼
前端·人工智能·后端
不才不才不不才5 小时前
Spring 源码系列(29): 20 道高频面试题源码级解析合集
java·后端·spring
索隆zoro5 小时前
Army 的可插拔架构:army-jdbc 与方言模块
java·后端
小蒜学长5 小时前
校园社团招新网站的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·校园社团招新网站
蜗牛互联网7 小时前
从Barclays扩大Claude部署看受监管软件工程的责任重分配
java·大数据·人工智能·后端·软件工程
周杰伦fans8 小时前
C#对话摘要降低Token消耗
人工智能·后端·c#
做系统的大强8 小时前
脚本说 PASS、OS 读全零 —— 到底是谁在撒谎?
后端·操作系统
code2cat9 小时前
【随笔】MCP资源更新订阅:通知到达以后,Agent怎样刷新旧资料
java·后端·开发工具·ai agent·mcp
福兮说10 小时前
Go slices 包的七个坑:Delete 把旧切片清零、Insert 时而写穿、SortFunc 不稳定(Go 1.25 实跑)
go