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

Go源代码编译运行(Window环境),第1张

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

文章目录 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文件

1.gcc安装 1.1gcc下载

根据自己的系统选择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.git
2.2注意事项 1.5版本以后的Go编译需要本地已经安装过1.4版本以上的go,所以如果本机没有的话,请去下载一个 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源码

进入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:\GoLearnSource\go\bin> .\go.exe  run .\whatTest.go

调用成功

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/989719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存