1. 如果项目编译过程中出现 "package XXX is not in GOROOT” when building Golang project" 这样的问题,用go mod init 命令添加到项目到GOROOT中即可。
问题来了, go mod init究竟有什么作用?
官方文档中的说法是
The go mod init
command creates a go.mod file to track your code's dependencies. So far, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go.mod file will list the versions your code depends on. This keeps builds reproducible and gives you direct control over which module versions to use.
简单翻译过来就是,go mod 是一个包管理工具,包括了你需要的第三方的包的版本,如果需要升级某个第三方包的版本,修改go mod中对应包的版本号,然后go mod tidy命令,功能是add missing and remove unused modules。go.mod文件一旦创建后,它的内容将会被go toolchain全面掌控。go toolchain会在各类命令执行时,比如go get、go build、go mod等修改和维护go.mod文件。
go mod init 负责把当前路径设为goroot路径.
go.mod负责记录需求列表(用于构建依赖模块)