且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导入之前我必须先拥有文件没有任何意义

更新时间:2022-11-30 16:22:26

Go是一种静态类型语言,因此它需要在编译时解析对外部包的任何引用. "go"工具期望外部软件包的源位于本地可访问的路径,因此您需要使用"go get"下载它们.

Go is a static type language thus it needs to resolve any reference to external package at compile time. The "go" tool expects the source of external packages in locally accessible path thus you need to use "go get" to download them.

根据您的描述,您可能未设置GOPATH.使用ECHO $ GOPATH检查设置是否正确.

From what you described, you probably did not set the GOPATH. Use ECHO $GOPATH to check if it is set correctly.

对于我的GO项目,我通常使用GOPATH作为工作区,类似于Python中的virtualenv或Ruby中的rbenv/rvm.假设我的项目"myproject"的根目录为/projects/myproject,我的源文件位于/projects/myproject/src/myproject,并且导入了"github.com/user/project",然后

For my GO project, I normally use GOPATH as workspace, similar to virtualenv in Python or rbenv/rvm in Ruby. Let say my project "myproject" has root at /projects/myproject, my source file will be located at /projects/myproject/src/myproject and there is an import of "github.com/user/project", then

> cd /projects/myproject
> export GOPATH=`pwd`          # or export GOPATH=/projects/myproject
> go get github.com/user/project

执行"go get"命令后,"github.com/user/project"的源将下载到/projects/myproject/src/github.com/user/project.

After "go get" command, the source of "github.com/user/project" will be downloaded to /projects/myproject/src/github.com/user/project.

当您使用进行构建"或进行安装"时,它将作为外部软件包位于$ GOPATH/src文件夹中进行编译.

When you use "go build" or "go install" then, it will compile as the external packages is in the $GOPATH/src folder.

如果在默认文件夹中安装Go,则需要在PATH环境变量中包括Go已安装的bin文件夹.之后,GOPATH是您需要使用"go"工具的另一个环境变量.

If you install Go in the default folder, you need to include Go installed bin folder in the PATH environment variable. After that GOPATH is the other environment variable you need for "go" tool to work.