[Go] 编译调试源代码示例
环境:centos7、go1.15
在github上拉取源代码
不用在意版本问题,个人是fork到自己的仓库中了
(base) [root@VM-15-185-centos /src]# git clone https://github.com/coderhu1024/go.git
Cloning into 'go'...
remote: Enumerating objects: 477545, done.
remote: Counting objects: 100% (1019/1019), done.
remote: Compressing objects: 100% (631/631), done.
remote: Total 477545 (delta 460), reused 822 (delta 378), pack-reused 476526
Receiving objects: 100% (477545/477545), 255.93 MiB | 6.93 MiB/s, done.
Resolving deltas: 100% (379680/379680), done.
修改原生库的函数fmt.Println()
println
是 Go 语言运行时提供的内置方法,它不需要依赖任何包就可以向标准输出打印字符串,也便于自己写测试文件
print.go文件位于
(base) [root@VM-15-185-centos /src/go/src/fmt]# pwd
/src/go/src/fmt
修改后
func Println(a ...interface{}) (n int, err error) {
println("herrhu go") //新添加内容
return Fprintln(os.Stdout, a...)
}
重新编译go源代码
./src/make.bash
脚本会编译 Go 语言的二进制、工具链以及标准库和命令并将源代码和编译好的二进制文件移动到对应的位置上
(base) [root@VM-15-185-centos /src/go/src]# ./make.bash
Building Go cmd/dist using /usr/lib/golang. (go1.15.5 linux/amd64)
Building Go toolchain1 using /usr/lib/golang.
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 linux/amd64.
---
Installed Go for linux/amd64 in /src/go
Installed commands in /src/go/bin
测试
export GOROOT=/src/go
先将GOROOT环境变量放在git拉的代码那里,否则就用了本机yum安装的默认GOROOT /usr/lib/golang
(base) [root@VM-15-185-centos /src]# cat main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
编写测试demo
注意一定要使用git拉去后,修改go源代码后编译出的go可执行文件
(base) [root@VM-15-185-centos /src]# /src/go/bin/go run main.go
herrhu go
Hello World!
测试成功!