Go包的安装位置由哪些规则决定?

huangapple go评论72阅读模式
英文:

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 to pkg rather than bin?
  • Where does the tool part in the pkg\tool come from? Can I do similar thing for my own package?

答案1

得分: 3

go build 命令依赖于一个 go 工具目录,其中安装了构建工具(compile.exelink.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\

huangapple
  • 本文由 发表于 2016年11月30日 10:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/40879451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定