go_compiler

Resolving a package to a module

bash 复制代码
When the go command loads a package using a package path, it needs to determine which module provides the package.
If no modules provide the package or if two or more modules provide the package, the go command reports an error.
The -mod=mod flag instructs the go command to attempt to find new modules providing missing packages and to update
go.mod and go.sum.
The go get and go mod tidy commands do this automatically.
#通过GOPROXY中的地址下载module依赖
When the go command looks up a new module for a package path, it checks the GOPROXY environment variable, which is
a comma-separated list of proxy URLs or the keywords direct or off.
A proxy URL indicates the go command should contact a module proxy using the GOPROXY protocol.
direct indicates that the go command should communicate with a version control system.
off indicates that no communication should be attempted.
#寻找可能提供此package的module
For each entry in the GOPROXY list, the go command requests the latest version of each module path that might
provide the package (that is, each prefix of the package path).
For each successfully requested module path, the go command will download the module at the latest version and check
whether the module contains the requested package.
If no modules are found, the go command tries the next entry in the GOPROXY list.
#成功找到
After a suitable module has been found, the go command will add a new requirement with the new module's path
and version to the main module's go.mod file.

go.mod文件

bash 复制代码
require指令
#require module-path module-version
#Declares a module as a dependency of the current module, specifying the minimum version of the module required.
#For each required module version, the go command loads the go.mod file for that version and incorporates the
#requirements from that file.
#indirect表示该module中的package没有被项目直接依赖
#An // indirect comment indicates that no package from the required module is directly imported by any package in
#the main module.

compiler

bash 复制代码
gc是Go compiler,GC是garbage collection
go tool compile -h
-gcflags=-lang=go1.22,会将参数传给go tool compile.

The build flags are shared by the build, clean, get, install, list, run, and test commands:
-x,print the commands.(可以查看go build,run,clean的执行细节)
-asmflags,arguments to pass on each go tool asm invocation.
-gcflags,arguments to pass on each go tool compile invocation.
-ldflags,arguments to pass on each go tool link invocation.
The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a space-separated list of arguments to pass to an
underlying tool during the build.

https://go.dev/ref/mod

https://go.dev/doc/modules/gomod-ref

https://pkg.go.dev/cmd/go@go1.22.3

https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/cmd/compile/;bpv=0

https://github.com/golang/go/blob/master/src/cmd/compile/README.md

https://www.reddit.com/r/golang/comments/16okuwb/can_you_explain_to_me_what_is_gopath/

https://www.yasint.dev/gopath-goroot

相关推荐
张忠琳2 小时前
【Go 1.26.4】Golang Map 深度解析
开发语言·后端·golang
何以解忧,唯有..8 小时前
Go 语言安装与环境配置完整指南
开发语言·后端·golang
踏着七彩祥云的小丑8 小时前
Go 学习第6天:结构体 + 切片 + range遍历
开发语言·学习·golang·go
浮尘笔记9 小时前
Go实现大文件异步流式采集引擎
开发语言·后端·golang
l齐天10 小时前
Ubuntu 中编译 Go + PBC 程序为 Windows 11 可运行文件
windows·ubuntu·golang
jieyucx10 小时前
《Go 数据库编程开篇:彻底打通 database/sql 与 MySQL 驱动的连接池调优密码》
数据库·sql·golang
壮Sir不壮10 小时前
GO语言——GMP调度模型
linux·开发语言·golang·go·操作系统·线程·协程
再玩一会儿看代码11 小时前
2026 年 ChatGPT 套餐怎么选?Free、Go、Plus、Pro、Business、Enterprise 一次讲清楚
人工智能·gpt·chatgpt·golang·openai·codex
必胜刻11 小时前
Go项目实战:使用Ollama本地部署大模型实现AI智能笔记生成
人工智能·笔记·ai·语言模型·golang
小小龙学IT1 天前
Go 泛型深度解析:从设计哲学到工程实践
服务器·数据库·golang