Go, Golang: 使用 GOROOT 导入外部包。

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

Go, Golang : external package import with GOROOT

问题

我正在尝试进入下一步,但一直遇到错误。

  1. 我已经准备好要导入的包。
  2. 我只需要从GitHub导入外部包,并能够在任何代码中使用它。

所以我做了以下操作。

mkdir $HOME/go
export GOPATH=$HOME/go
go get github.com/user/project

这个命令成功运行了。我从GitHub下载了它,并将源代码文件放在这里。

/Users/user/go/src/github.com/user/project/project.go

所以为了使用我刚刚导入的这个包,我运行了以下命令:

go run /Users/user/Desktop/code.go

但是我得到了以下错误:

 MacBook-Air:~ user$ go run /Users/user/Desktop/code.go 
 Desktop/code.go:32:8: cannot find package "project" in any of:
        /usr/local/go/src/pkg/project (from $GOROOT)
        /Users/user/go/src/project (from $GOPATH)

我应该怎么办?我有什么遗漏吗?提前感谢你的帮助。我写了很多代码,但由于这个问题无法分发它,非常沮丧。

英文:

https://stackoverflow.com/questions/19073794/go-golang-does-not-make-sense-that-i-have-to-have-files-before-import

I am trying to go to next step but keep getting errors

  1. I have the package that I want to import ready.
  2. All I need to do is to import the external package from github and be able to use it on any code.

So this is what I did.

mkdir $HOME/go
export GOPATH=$HOME/go
go get github.com/user/project

This runs successfully. I downloaded it onto here with source code files from github

/Users/user/go/src/github.com/user/project/project.go

So to use this package that I just import I do

go run /Users/user/Desktop/code.go

But I am getting the following errors

 MacBook-Air:~ user$ go run /Users/user/Desktop/code.go 
 Desktop/code.go:32:8: cannot find package "project" in any of:
        /usr/local/go/src/pkg/project (from $GOROOT)
        /Users/user/go/src/project (from $GOPATH)

What should I do? AM I missing something? Thanks in advance and please help me. I wrote a lot of code but being very frustrated not being able to distribute it because of this.

答案1

得分: 3

错误信息显示在你的 code.go 文件的第32行找不到包 "goling"。

假设这是你想要使用的本地包,你需要确保它在你的 GOPATH 中。

如果你设置了 GOPATH,那么你应该在其中开发你的代码,所以将 "goling" 目录移动到 /Users/user/go/src 是正确的做法。

另外,"goling" 可能是一个拼写错误,所以请检查 code.go 中的导入部分。如果你想从 GitHub 导入一个项目,导入语句应该是这样的:

import "github.com/user/project"

然后你可以使用项目的部分,前缀为 project.

如果这些方法都不能帮助你解决问题,那么请将 code.go 的导入部分发布出来。

英文:

The error message says at line 32 in your code.go it can't find package "goling".

Assuming that is a local package you want to use, you need to make sure it is in your GOPATH.

If you set GOPATH then you should develop your code within it, so moving the "goling" directory into /Users/user/go/src is the right thing to do.

Alternatively "goling" could be a typo, so check the imports in code.go. If you want to import an project from github the import should say

import "github.com/user/project"

And you then use the parts of project with a prefix of project.

If that doesn't help you get it working, then post the imports section of code.go.

答案2

得分: 1

看起来你把外部包放在了与使用它的主要包相同的文件夹中。在Go语言中,所有的包必须放在不同的目录中。看起来GitHub项目本身实际上是这样做的。如果你把这些包分别放在不同的目录中,它应该可以正常工作。

英文:

It looks like you've got the external package in the same folder as your main package which uses it. In go, all packages must be in separate directories. It looks like the github project itself is actually doing that. If you separate the packages into different directories it should work properly.

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

发表评论

匿名网友

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

确定