- Go源代码编译运行(Window环境)
- 1.gcc安装
- 1.1gcc下载
- 1.2gcc安装
- 2.Go源码安装
- 2.1下载Go源代码
- 2.2注意事项
- 2.3安装完1.4后
- 2.4安装Go源码
- 3.修改Go源码并调用
- 3.1修改源代码
- 3.2 编写GO代码调用`fmt.Println`函数
- 3.3 使用编译后的源代码启动go文件
根据自己的系统选择gcc下载,笔者的系统为win10 64位 选择了x86_64-posix-seh
MinGW-w64 - for 32 and 64 bit Windows - Browse /mingw-w64/mingw-w64-snapshot at SourceForge.net
1.2gcc安装将下载后端压缩包解压到自己想放的位置,随后配置环境变量,指向bin目录即可
可以使用 gcc -v 验证gcc是否安装成功
2.Go源码安装 2.1下载Go源代码git clone https://github.com/golang/go.git2.2注意事项
- 1.5版本以后的Go编译需要本地已经安装过1.4版本以上的go,所以如果本机没有的话,请去下载一个 https://studygolang.com/dl/golang/go1.14.3.windows-amd64.msi
设置环境变量
set CGO_ENABLED=0 set GOROOT_BOOTSTRAP=本地安装好的go路径,例如`C:Go` set GOROOT_FINAL=本地安装好的go路径,例如`C:Go`2.4安装Go源码
进入clone下来的Go源码中的src目录,执行 all.bat,耐心等待编译。
3.修改Go源码并调用 3.1修改源代码比如修改 fmt.print.go 中的Println()函数
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Println(a ...any) (n int, err error) {
println("fmt") // 添加一行输出
return Fprintln(os.Stdout, a...)
}
然后进入GO源码 src目录下,调用 make.bat 编译源码
注意,编译完成后要修改GOROOT环境变量为GO源码所在的位置
3.2 编写GO代码调用fmt.Println函数为了方便可以将此.go文件放置在Go源码目录下的bin目录中
package main
import "fmt"
func main() {
fmt.Println("try")
}
3.3 使用编译后的源代码启动go文件
PS F:GoLearnSourcegobin> .go.exe run .whatTest.go
调用成功



