很多语言都有自己的运行时环境,go自然也不例外,那么今天我们就来讲讲go语言的运行时环境!
我们都知道Java的运行时环境是jvm ,javascript的运行时环境是浏览器内核
Java -->jvm
javascript-->浏览器内核
他们一般的运行流程可以归纳为:code-->bytecode-->runtime-->os
那么go的运行时环境是什么样呢?
go没有虚拟机的概念,go的runtime是代码,Runtime作为程序的一部分打包进二进制产物,Runtime随用户程序一起运行,Runtime与用户程序没有明显界限,直接通过函数调用!
比如:
一些go的关键字其实是Runtime下的函数:
关键字 函数
go newproc
new newobject
make makeslice, makechain, makemap...
<- chansend1, chanrecv1
这样就屏蔽了系统调用!
进入目标路径输入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
说明编译的时候自带运行环境
词法分析--->句法分析--->语义分析--->中间码生成-->代码优化--->机器码生成--->链接
注意💡:编译过程都干了些啥事可以去看看编译原理,中间码是平台无关的,机器码是平台相关的!