导入之前我必须先拥有文件没有任何意义 [英] Does not make sense that I have to have files before import

查看:63
本文介绍了导入之前我必须先拥有文件没有任何意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I import an external package from scratch?

我已经在Go中编写了一个库包,并进行了测试以通过github进行分发.我正在关注 http://golang.org/doc/code.html 并使用mac但错误消息

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.

$ 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

转到install仍然出现错误

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

我不明白为什么我们需要文件才能在Go中导入外部软件包.外部软件包意味着我得到了一些东西,并从外部软件包创建文件.

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

这给了我未设置$ GOPATH". 作为初学者,我为建立开始工作"的环境而感到沮丧.提前非常感谢.

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.

摘要

   How do I import an external package from scratch?

推荐答案

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.

这篇关于导入之前我必须先拥有文件没有任何意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆