我不明白为什么在导入之前必须要有文件。

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

Does not make sense that I have to have files before import

问题

如何从头开始导入外部包?

我已经用Go编写了一个库包,并希望通过GitHub进行分发。我正在遵循http://golang.org/doc/code.html,并在Mac上使用,但是出现错误消息。

我输入的命令如下:

$ mkdir $HOME/go
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin
$ mkdir -p $GOPATH/src/github.com/user
$ mkdir $GOPATH/src/github.com/user/project

然后我输入:

$ go get github.com/user/project

仍然出现"go install"的错误:

can't load package: package github.com/golingorg/goling: no Go source files in /Users/user_name/go/src/github.com/user/project

我不明白为什么我们需要文件来导入Go中的外部包。外部包意味着我获取某些内容,并从外部包中创建文件。

我的问题是如何从头开始导入外部包。大多数文档只是简单地说:

go get github.com/yasushi-saito/fifo_queue

这给出了"$GOPATH not set."的错误。作为一个初学者,我对为"go get"设置环境感到非常沮丧。非常感谢您的帮助。

总结:

如何从头开始导入外部包?

英文:

How do I import an external package from scratch?

I've written a library package in Go and testing to distribute through github. I am following http://golang.org/doc/code.html and using mac but getting error message

cmd I put is following.

$ mkdir $HOME/go
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin
$ mkdir -p $GOPATH/src/github.com/user
$ mkdir $GOPATH/src/github.com/user/project

Then I put

$ go get github.com/user/project

Still errors with go install

can't load package: package github.com/golingorg/goling: no Go source files in /Users/user_name/go/src/github.com/user/project

I do not understand why we need files to import an external package in Go. External package means that I get something and create files from the external package.

My question is how I import an external package from scratch. Most of documents just say something like

 go get github.com/yasushi-saito/fifo_queue

this gives me "$GOPATH not set."
I am getting frustrated setting up the environment for "go get" to work, as a beginner. Thanks a lot in advance.

Summary

   How do I import an external package from scratch?

答案1

得分: 5

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

根据您的描述,您可能没有设置GOPATH。使用ECHO $GOPATH检查它是否设置正确。

对于我的Go项目,我通常将GOPATH用作工作空间,类似于Python中的virtualenv或Ruby中的rbenv/rvm。假设我的项目"myproject"的根目录位于/projects/myproject,我的源文件将位于/projects/myproject/src/myproject,并且有一个导入"github.com/user/project",然后:

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

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

当您使用"go build"或"go install"时,它将编译为$GOPATH/src文件夹中的外部包。

如果您将Go安装在默认文件夹中,则需要将Go安装的bin文件夹包含在PATH环境变量中。之后,GOPATH是"go"工具正常工作所需的另一个环境变量。

英文:

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.

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

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

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

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

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.

答案2

得分: 5

这是我完成的方法:

1. 首先设置你的工作空间

 mkdir $HOME/go
 export GOPATH=$HOME/go
 export PATH=$PATH:$GOPATH/bin

2. 创建项目

 mkdir -p $GOPATH/src/github.com/user
 mkdir $GOPATH/src/github.com/user/hello
 touch $GOPATH/src/github.com/user/hello/hello.go

3. 安装项目

go install github.com/user/hello

4. 运行项目

cd $GOPATH/bin
./hello

我使用了以下的vagrant镜像:https://github.com/dcoxall/vagrant-golang

英文:

That's how I done it:

1. Setup your workspace first

 mkdir $HOME/go
 export GOPATH=$HOME/go
 export PATH=$PATH:$GOPATH/bin

2. Create the project

 mkdir -p $GOPATH/src/github.com/user
 mkdir $GOPATH/src/github.com/user/hello
 touch $GOPATH/src/github.com/user/hello/hello.go

3. Install it

go install github.com/user/hello

4. Run it

cd $GOPATH/bin
./hello

I used the following vagrant image: https://github.com/dcoxall/vagrant-golang

答案3

得分: 2

根据go get的帮助输出,它说:

> 默认情况下,get会使用网络来检查缺失的包,但不会使用网络来查找现有包的更新。

当你在运行go get之前创建了$GOPATH/src/github.com/user/project目录,它会假设该包已经被下载,所以跳过了构建和安装包的步骤。这导致失败,因为该目录中没有Go源文件。

所以简单的解决方法是不要创建与你尝试下载的包相关联的文件夹:go get会为你创建该文件夹。

英文:

From the help output for go get, it says:

> By default, get uses the network to check out missing packages but does not use it to look for updates to existing packages.

When you created the $GOPATH/src/github.com/user/project directory prior to running go get, it assumed that the package had already been downloaded so skipped to the step of trying to build and install the package. That failed because the directory contained no Go source files.

So the simple fix is to not create the folder associated with the package you are trying to download: go get will do that for you.

huangapple
  • 本文由 发表于 2013年9月29日 10:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/19073794.html
匿名

发表评论

匿名网友

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

确定