英文:
What rules decides the Go package installation location?
问题
当我在%GOPATH%\src
中运行go install
命令时,我的一些自定义包会被安装到%GOPATH%\pkg
目录下。
我了解到%GOROOT%
和%GOPATH%
文件夹有类似的组织结构。所以我尝试了在%GOROOT%\src\cmd\cgo
目录下运行go install cmd\cgo
命令,该目录是golang安装的一部分。但最终生成的cgo.exe
被安装到了%GOROOT%\pkg\tool\
目录下。
我检查了cmd\cgo
目录下的所有*.go
文件,它们都有一个package main
的声明。所以我本来期望最终的cgo.exe
被安装到%GOROOT%\bin
目录下。
我的问题是:
- 为什么
cgo.exe
被安装到pkg
目录而不是bin
目录? pkg\tool
中的tool
部分是从哪里来的?我能否对我的自定义包做类似的操作?
英文:
When I run go install
with some of my own package located in %GOPATH%\src
, it will be installed to %GOPATH%\pkg
.
I read that %GOROOT%
and %GOPATH%
folders have similar organization. So I tried go install cmd\cgo
with the %GOROOT%\src\cmd\cgo
package which is part of the golang installation. But the final cgo.exe
is installed to %GOROOT%\pkg\tool\
.
I checked all the *.go
files in the cmd\cgo
folder. They all have a package main
declaration. So I was expecting the final cgo.exe
to be installed to %GOROOT%\bin
.
My questions are:
- Why the
cgo.exe
is installed topkg
rather thanbin
? - Where does the
tool
part in thepkg\tool
come from? Can I do similar thing for my own package?
答案1
得分: 3
go build
命令依赖于一个 go 工具目录,其中安装了构建工具(compile.exe
、link.exe
等)。
而 ToolDir
被定义为:
var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
因此,任何构建工具都位于 %GOROOT%\pkg\tool\
目录下。
英文:
The go build
command relies on a go tool directory, where build tools are installed (compile.exe
, link.exe
, ...).
And ToolDir
is defined as:
var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
So any build tool is in %GOROOT%\pkg\tool\
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论