Go如何定位导入的包?

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

How does Go locate imported packages?

问题

我是新手学习Go语言,根据如何编写Go代码准备好了我的工作空间。

但是现在我对于go工具如何获取fmtstrings或者我自己的包的路径感到困惑。

在C语言中,我们通过-I提供头文件的路径,通过-L提供链接器的路径,但是在Go语言中,我们给出了两个路径GOHOMEGOPATH,而且这两个路径都是bin目录,没有指定包的路径。

英文:

I am new to Go, and I have prepared my work space according to the How to write go code

But now I am confused how go tool gets the path of packages like fmt, strings or my own packages.

As in C language we provide the path of header file by -I and linker path by -L but in Go we have given two paths GOHOME and GOPATH and both paths are of bin directories none of them specify the package path.

答案1

得分: 2

根据你提供的链接中所解释的,GOPATH(以及其他Go的位置)应该按照以下方式布局:一个用于编译库的"pkg"目录,以及一个用于库源代码的"src"目录。它们不仅仅是二进制文件目录。

英文:

As is explained in the article you linked, GOPATH (and other go locations) should be layed out with a "pkg" directory for compiled libraries, and a "src" directory for library sources. They are not just bin directories.

答案2

得分: 0

$GOPATH 是一个由冒号(在 Windows 上是分号)分隔的路径列表,用于存放 Go 工作区(具有 {bin,pkg,src} 结构)。当编译和链接你的程序时,Go 工具会自动在这些路径中查找包和源文件。

英文:

$GOPATH is a colon (or semicolon on Windows) separated list of paths to Go workspaces (with the {bin,pkg,src} structure). The go tools will automatically look for packages and source files in those paths when compiling and linking your programs.

huangapple
  • 本文由 发表于 2014年4月16日 21:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/23110408.html
匿名

发表评论

匿名网友

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

确定