编译Go语言源码

770 阅读4分钟

Go语言有好多官网

  1. go.dev/
  2. golang.org/
  3. golang.google.cn/

golang.org/ 会重定向到 golang.google.cn/

参考链接: golang.google.cn/doc/install…

下载源码

golang.google.cn/dl/

image.png

go1.17.src.tar.gz (21MB)

配置环境

编译Go工具链, 有很多方式. 具体参考 官方文档

这里以 Go语言 自举方式编译, 即使用 Go 编译 Go工具链

下载安装 Go 语言

image.png

go1.17.windows-amd64.msi (124MB)

解压源代码, 配置环境变量

image.png

这里不编译 cgo 相关, 所以

set CGO_ENABLED=0

设置Go Home目录

set GOROOT_BOOTSTRAP=C:\Program Files\Go

image.png

编译完成

H:\source\go\src>set CGO_ENABLED=0

H:\source\go\src>set GOROOT_BOOTSTRAP=C:\Program Files\Go

H:\source\go\src>all.bat  
Building Go cmd/dist using C:\Program Files\Go
Building Go toolchain1 using C:\Program Files\Go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for windows/amd64.


##### Testing packages.
ok      archive/tar     0.055s
ok      archive/zip     0.137s
ok      bufio   0.078s
ok      bytes   0.179s
ok      compress/bzip2  0.068s
ok      compress/flate  0.443s
ok      compress/gzip   0.032s
ok      compress/lzw    0.041s
ok      compress/zlib   0.039s
ok      container/heap  0.028s
ok      container/list  0.029s
ok      container/ring  0.034s
ok      context 0.260s
ok      crypto  0.029s
ok      crypto/aes      0.034s
ok      crypto/cipher   0.022s
ok      crypto/des      0.033s
ok      crypto/dsa      0.022s
ok      crypto/ecdsa    0.033s
ok      crypto/ed25519  0.069s
ok      crypto/ed25519/internal/edwards25519    4.309s
ok      crypto/ed25519/internal/edwards25519/field      1.890s
ok      crypto/elliptic 0.142s
ok      crypto/elliptic/internal/fiat   0.032s
ok      crypto/hmac     0.030s
ok      crypto/internal/subtle  0.025s
ok      crypto/md5      0.032s
ok      crypto/rand     0.032s
ok      crypto/rc4      0.046s
ok      crypto/rsa      0.173s
ok      crypto/sha1     0.024s
ok      crypto/sha256   0.034s
ok      crypto/sha512   0.026s
ok      crypto/subtle   0.028s
ok      crypto/tls      0.654s
ok      crypto/x509     15.392s
ok      database/sql    0.817s
ok      database/sql/driver     0.040s
ok      debug/dwarf     0.043s
ok      debug/elf       0.060s
ok      debug/gosym     0.038s
ok      debug/macho     0.032s
ok      debug/pe        1.649s
ok      debug/plan9obj  0.037s
ok      embed/internal/embedtest        0.034s
ok      encoding/ascii85        0.027s
ok      encoding/asn1   0.039s
ok      encoding/base32 0.068s
ok      encoding/base64 0.029s
ok      encoding/binary 0.049s
ok      encoding/csv    0.052s
ok      encoding/gob    0.055s
ok      encoding/hex    0.061s
ok      encoding/json   0.311s
ok      encoding/pem    0.045s
ok      encoding/xml    0.046s
ok      errors  0.035s
ok      expvar  0.029s
ok      flag    0.188s
ok      fmt     0.082s
ok      go/ast  0.034s
ok      go/build        1.568s
ok      go/build/constraint     0.027s
ok      go/constant     0.034s
ok      go/doc  0.129s
ok      go/format       0.045s
ok      go/importer     0.215s
ok      go/internal/gccgoimporter       0.111s
ok      go/internal/gcimporter  0.227s
ok      go/internal/srcimporter 1.331s
ok      go/parser       0.056s
ok      go/printer      0.312s
ok      go/scanner      0.028s
ok      go/token        0.054s
ok      go/types        3.059s
ok      hash    0.028s
ok      hash/adler32    0.035s
ok      hash/crc32      0.035s
ok      hash/crc64      0.040s
ok      hash/fnv        0.041s
ok      hash/maphash    0.171s
ok      html    0.039s
ok      html/template   0.137s
ok      image   0.079s
ok      image/color     0.041s
ok      image/draw      0.071s
ok      image/gif       0.502s
ok      image/jpeg      0.172s
ok      image/png       0.071s
ok      index/suffixarray       0.199s
ok      internal/abi    0.312s
ok      internal/cpu    0.024s
ok      internal/execabs        0.038s
ok      internal/fmtsort        0.034s
ok      internal/itoa   0.050s
ok      internal/poll   0.037s
ok      internal/profile        0.036s
ok      internal/reflectlite    0.113s
ok      internal/singleflight   0.036s
ok      internal/syscall/windows        0.050s
ok      internal/syscall/windows/registry       0.055s
ok      internal/trace  0.095s
ok      internal/unsafeheader   0.032s
ok      internal/xcoff  0.082s
ok      io      0.171s
ok      io/fs   0.047s
ok      io/ioutil       0.040s
ok      log     0.039s
ok      math    0.030s
ok      math/big        0.917s
ok      math/bits       0.050s
ok      math/cmplx      0.033s
ok      math/rand       0.191s
ok      mime    0.210s
ok      mime/multipart  0.251s
ok      mime/quotedprintable    0.117s
ok      net     6.375s
ok      net/http        17.712s
ok      net/http/cgi    0.538s
ok      net/http/cookiejar      0.051s
ok      net/http/fcgi   0.148s
ok      net/http/httptest       4.634s
ok      net/http/httptrace      0.025s
ok      net/http/httputil       0.786s
ok      net/http/internal       0.049s
ok      net/http/internal/ascii 0.025s
ok      net/http/pprof  5.318s
ok      net/internal/socktest   0.021s
ok      net/mail        0.058s
ok      net/rpc 0.099s
ok      net/rpc/jsonrpc 0.035s
ok      net/smtp        0.053s
ok      net/textproto   0.021s
ok      net/url 0.036s
ok      os      3.448s
ok      os/exec 5.012s
ok      os/signal       1.976s
ok      os/user 0.026s
ok      path    0.039s
ok      path/filepath   0.989s
ok      plugin  0.041s
ok      reflect 0.883s
ok      regexp  0.088s
ok      regexp/syntax   0.287s
ok      runtime 40.710s
ok      runtime/debug   0.044s
ok      runtime/internal/atomic 0.055s
ok      runtime/internal/math   0.022s
ok      runtime/internal/sys    0.027s
ok      runtime/metrics 0.043s
ok      runtime/pprof   8.405s
ok      runtime/trace   1.159s
ok      sort    0.079s
ok      strconv 0.352s
ok      strings 0.224s
ok      sync    0.788s
ok      sync/atomic     1.716s
ok      syscall 0.097s
ok      testing 1.352s
ok      testing/fstest  0.044s
ok      testing/iotest  0.026s
ok      testing/quick   0.119s
ok      text/scanner    0.025s
ok      text/tabwriter  0.032s
ok      text/template   0.069s
ok      text/template/parse     0.034s
ok      time    3.900s
ok      unicode 0.033s
ok      unicode/utf16   0.037s
ok      unicode/utf8    0.029s
ok      cmd/addr2line   5.261s
ok      cmd/api 14.252s
ok      cmd/asm/internal/asm    1.186s
ok      cmd/asm/internal/lex    0.030s
ok      cmd/compile/internal/dwarfgen   1.399s
ok      cmd/compile/internal/importer   2.023s
ok      cmd/compile/internal/ir 0.032s
ok      cmd/compile/internal/logopt     4.108s
ok      cmd/compile/internal/noder      0.312s
ok      cmd/compile/internal/ssa        0.470s
ok      cmd/compile/internal/ssagen     0.036s
ok      cmd/compile/internal/syntax     0.316s
ok      cmd/compile/internal/test       17.463s
ok      cmd/compile/internal/typecheck  1.932s
ok      cmd/compile/internal/types      0.081s
ok      cmd/compile/internal/types2     1.618s
ok      cmd/cover       6.305s
ok      cmd/doc 0.371s
ok      cmd/fix 5.444s
ok      cmd/go  44.782s
ok      cmd/go/internal/auth    0.261s
ok      cmd/go/internal/cache   4.432s
ok      cmd/go/internal/fsys    1.513s
ok      cmd/go/internal/generate        0.693s
ok      cmd/go/internal/get     0.650s
ok      cmd/go/internal/imports 0.358s
ok      cmd/go/internal/load    0.112s
ok      cmd/go/internal/lockedfile      0.269s
ok      cmd/go/internal/lockedfile/internal/filelock    0.131s
ok      cmd/go/internal/modconv 0.342s
ok      cmd/go/internal/modfetch        0.171s
ok      cmd/go/internal/modfetch/codehost       0.152s
ok      cmd/go/internal/modfetch/zip_sum_test   0.191s
ok      cmd/go/internal/modload 0.032s
ok      cmd/go/internal/mvs     0.119s
ok      cmd/go/internal/par     0.081s
ok      cmd/go/internal/search  0.062s
ok      cmd/go/internal/str     0.026s
ok      cmd/go/internal/test    0.067s
ok      cmd/go/internal/txtar   0.060s
ok      cmd/go/internal/vcs     0.042s
ok      cmd/go/internal/web     0.031s
ok      cmd/go/internal/work    0.123s
ok      cmd/gofmt       0.360s
ok      cmd/internal/archive    0.508s
ok      cmd/internal/buildid    0.335s
ok      cmd/internal/dwarf      0.049s
ok      cmd/internal/edit       0.051s
ok      cmd/internal/goobj      0.036s
ok      cmd/internal/moddeps    3.664s
ok      cmd/internal/obj        0.157s
ok      cmd/internal/obj/ppc64  0.247s
ok      cmd/internal/obj/riscv  0.293s
ok      cmd/internal/obj/s390x  0.025s
ok      cmd/internal/obj/x86    3.835s
ok      cmd/internal/objabi     0.024s
ok      cmd/internal/pkgpath    0.181s
ok      cmd/internal/src        0.025s
ok      cmd/internal/sys        0.023s
ok      cmd/internal/test2json  0.162s
ok      cmd/link        6.022s
ok      cmd/link/internal/benchmark     0.040s
ok      cmd/link/internal/ld    9.094s
ok      cmd/link/internal/loader        0.465s
ok      cmd/nm  3.139s
ok      cmd/objdump     3.874s
ok      cmd/pack        12.414s
ok      cmd/pprof       2.784s
ok      cmd/trace       2.501s
ok      cmd/vet 11.559s

##### os/user with tag osusergo
ok      os/user 0.075s

##### ../test/bench/go1

##### ../test
# go run run.go -- fixedbugs/issue27836.go
exit status 1
H:\source\go\test\fixedbugs\issue27836.dir\main.go:6:2: could not import "Äfoo": file not found

FAIL    fixedbugs\issue27836.go 0.129s
2021/08/21 17:23:16 Failed: exit status 1

##### API check
Go version is "go1.17", ignoring -next H:\source\go\api\next.txt
go tool dist: FAILED

H:\source\go\src>

对比编译前后

image.png

image.png

image.png