Go Golang:没有意义,我必须在导入之前有文件 [英] Go, Golang : does not make sense that I have to have files before import

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

问题描述

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



我在Go上编写了一个库包,并通过github进行测试。我遵循 http://golang.org/doc/code.html 并使用mac,但获取错误信息

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

然后我把 p>

  $ go get github.com/user/project 

仍然出现安装错误

 无法加载包:package github.com / golingorg / goling:no在/Users/user_name/go/src/github.com/user/project中找到源文件

我不明白为什么我们需要文件在Go中导入外部软件包。外部软件包意味着我可以从外部软件包中获取内容并创建文件。



我的问题是如何从头开始导入外部软件包。大多数文件只是说一些像

  go get github.com/yasushi-saito/fifo_queue 

这给了我$ GOPATH未设置。
作为一个初学者,我越来越沮丧地设置去获取工作的环境。
$ b 小结

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


解决方案

Go是一种静态类型语言,因此需要解决在编译时对外部程序包的任何引用。 go工具需要本地可访问路径中的外部软件包源,因此您需要使用go get来下载它们。

从你所描述的,你可能没有设置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`#or 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在PATH环境变量中安装了bin文件夹。之后,GOPATH是您需要使用去工具工作的其他环境变量。


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?

解决方案

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.

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

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