Go源代码编译运行(Window环境)

1.gcc安装

1.1gcc下载

x86_64-posix-seh

1.2gcc安装

将下载后端压缩包解压到自己想放的位置,随后配置环境变量,指向bin目录即可

可以使用 gcc -v 验证gcc是否安装成功

2.Go源码安装

2.1下载Go源代码

git clone https://github.com/golang/go.git

2.2注意事项

https://studygolang.com/dl/golang/go1.14.3.windows-amd64.msi

2.3安装完1.4后

设置环境变量

set CGO_ENABLED=0
set GOROOT_BOOTSTRAP=本地安装好的go路径,例如`C:\Go`
set GOROOT_FINAL=本地安装好的go路径,例如`C:\Go`

2.4安装Go源码

srcall.bat

3.修改Go源码并调用

3.1修改源代码

fmt.print.goPrintln()
// 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...)
}
srcmake.bat

注意,编译完成后要修改GOROOT环境变量为GO源码所在的位置

fmt.Println

为了方便可以将此.go文件放置在Go源码目录下的bin目录中

package main

import "fmt"

func main() {
	fmt.Println("try")
}

3.3 使用编译后的源代码启动go文件

PS F:\GoLearnSource\go\bin> .\go.exe  run .\whatTest.go

调用成功