项目作者: liveball

项目描述 :
read go std src code
高级语言: Go
项目地址: git://github.com/liveball/gosrc.git
创建时间: 2018-11-02T02:27:11Z
项目社区:https://github.com/liveball/gosrc

开源协议:Other

下载


深入go 源码学习

使用GODEV 跑标准库的test

1、设置GODEV 为自己的源码目录

$ GODEV= go 的绝对路径 eg: /data/app/go/src/gosrc/go

2、进入源码目录

$ cd $GODEV/src

3、设置引导安装到当前安装目录

use /

编译所有,包括单元测试

$ GOROOT_BOOTSTRAP=./ GO_GCFLAGS="-N -l" ./all.bash

编译

$ GOROOT_BOOTSTRAP=./ GO_GCFLAGS="-N -l" ./make.bash

不能 -+ compile: cannot disable optimizations while compiling runtime

use GOROOT

$ GOROOT_BOOTSTRAP=$(go env GOROOT) ./make.bash

$ GOOS=darwin GOARCH=amd64 ./bootstrap.bash

4、解决use of internal package cmd/internal/obj not allowed

GOROOT=/data/app/go/src/gosrc/go && export PATH=$GOROOT/bin:$PATH

4.1 default goroot

GOROOT=/usr/local/go && export PATH=$GOROOT/bin:$PATH

go test -v -run=Dynlink

5、go build go
$GODEV/bin/go run main.go build -debug-actiongraph="json"

展示整个编译链接的运行过程

go build -v -x -work -o main main.go

-v 会打印所编译过的包名字
-x 打印编译期间所执行的命令
-work 打印编译期间生成的临时文件路径,并且编译完成之后不会被删除

调试 cmd/go 进程启动

$GODEV/bin/go run /data/app/go/src/gosrc/go/src/cmd/go/main.go run /data/app/go/src/gosrc/demo/runtime/gc/gctodo/main.go

调试 cmd/compile 编译器

$GODEV/bin/go run /data/app/go/src/gosrc/go/src/cmd/compile/main.go -o main.o /data/app/go/src/gosrc/demo/type/type_definitions/method_set/main.go

$GODEV/bin/go run /data/app/go/src/gosrc/go/src/cmd/compile/main.go -o main.o /data/app/go/src/gosrc/demo/base_learn/slice/perform/main.go

GOROOT=/data/app/go/src/gosrc/go && export PATH=$GOROOT/bin:$PATH
go run /data/app/go/src/gosrc/go/src/cmd/link/main.go -o main main.o

  1. Building Go cmd/dist using /usr/local/go.
  2. Building Go toolchain1 using /usr/local/go.
  3. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
  4. Building Go toolchain2 using go_bootstrap and Go toolchain1.
  5. Building Go toolchain3 using go_bootstrap and Go toolchain2.
  6. Building packages and commands for darwin/amd64.
  7. ---
  8. ##### API check
  9. Go version is "go", ignoring -next /data/app/go/src/go/api/next.txt
  10. ALL TESTS PASSED
  11. ---
  12. Installed Go for darwin/amd64 in /data/app/go/src/go
  13. Installed commands in /data/app/go/src/go/bin
  14. *** You need to add /data/app/go/src/go/bin to your PATH.

4、使用新编译的工具链运行所有测试

4.1 使用mod作为包管理工具
export GO111MODULE=on

4.2 默认使用当前目录名作为当前包名的mod,如果需要重命名则删除存在的mod,重新生成
$GODEV/bin/go mod init runtime

4.3、进入某个标准包下面运行单个测试

$GODEV/bin/go test -v -run=TestYieldLocked

  1. === RUN TestYieldLockedProgress
  2. --- PASS: TestYieldLockedProgress (0.01s)
  3. === RUN TestYieldLocked
  4. --- PASS: TestYieldLocked (0.01s)
  5. PASS
  6. ok runtime 0.031s

$GODEV/bin/go test -run=NONE -v -bench=ChanProdCons0

  1. goos: darwin
  2. goarch: amd64
  3. pkg: runtime
  4. BenchmarkChanProdCons0-4 3000000 471 ns/op
  5. PASS
  6. ok runtime 1.919s
  7. `$GODEV/bin/go test -v -bench=. -run=Benchmark`
  8. ```
  9. goos: darwin
  10. goarch: amd64
  11. pkg: runtime
  12. BenchmarkMakeChan/Byte-4 30000000 47.3 ns/op
  13. BenchmarkMakeChan/Int-4 20000000 54.2 ns/op
  14. BenchmarkMakeChan/Ptr-4 10000000 130 ns/op
  15. BenchmarkMakeChan/Struct/0-4 30000000 41.3 ns/op
  16. ```
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Println("read go")
  7. }