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

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

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

相关推荐

  1. docker底层原理二:容器运行环境

    2024-04-26 00:42:02       54 阅读
  2. Go语言网络编程介绍以及案例运用

    2024-04-26 00:42:02       54 阅读
  3. 编程语言运行和垃圾回收机制概览

    2024-04-26 00:42:02       56 阅读
  4. C#获取当前运行环境信息

    2024-04-26 00:42:02       55 阅读
  5. JVM运行数据区概述以及分别存放内容

    2024-04-26 00:42:02       50 阅读
  6. go并发编程以及socket通信理解

    2024-04-26 00:42:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 00:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 00:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 00:42:02       87 阅读
  4. Python语言-面向对象

    2024-04-26 00:42:02       96 阅读

热门阅读

  1. go | 切片的长度和容量

    2024-04-26 00:42:02       34 阅读
  2. MySQL数据库管理DDL语言和数据库管理

    2024-04-26 00:42:02       34 阅读
  3. vue3中a-select的模糊查询

    2024-04-26 00:42:02       36 阅读
  4. 蚂蚁 2025届暑期实习 多模态LLM 面经

    2024-04-26 00:42:02       36 阅读
  5. Linux第三章

    2024-04-26 00:42:02       30 阅读
  6. 智能合约语言(eDSL)—— 并行化方案

    2024-04-26 00:42:02       28 阅读
  7. Leetcode 150:逆波兰表达式求值

    2024-04-26 00:42:02       30 阅读
  8. CDN的原理

    2024-04-26 00:42:02       25 阅读