我有一个'example.go'文件,我正在尝试导入同一文件夹中的目录。
我在同一个名为'project'的文件夹中有'example.go'和'lucky'目录。
这是我试图导入'幸运'目录的方式:
import ( "fmt" golucky "goLucky" "io/ioutil" "os" )但是当我运行example.go时,看起来它试图从go源导入它,因为它会抛出错误:
cannot find package "goLucky" in any of: /usr/local/go/src/pkg/goLucky (from $GOROOT) ($GOPATH not set)如何在与文件相同的目录中导入本地文件夹?
I have an 'example.go' file where I'm trying to import a directory in the same folder.
I have 'example.go' and the 'lucky' dir in the same folder called 'project'.
Here's how I'm trying to import the 'lucky' dir:
import ( "fmt" golucky "goLucky" "io/ioutil" "os" )But when I run example.go, it looks like it is trying to import it from the go source because it throws the error:
cannot find package "goLucky" in any of: /usr/local/go/src/pkg/goLucky (from $GOROOT) ($GOPATH not set)How can I import the local folder in the same dir as the file?
最满意答案
您需要设置GOPATH 环境变量并在其中找到您的lucky目录。 请参阅http://golang.org/doc/code.html#Organization
例如,如果您设置GOPATH=~ ,那么将您的lucky.go文件放在~/src/lucky/lucky.go那么您应该能够成功import "lucky" 。
You need to set your GOPATH environment variable and locate your lucky dir within that. See http://golang.org/doc/code.html#Organization
So for example, if you set GOPATH=~, then put your lucky.go file in ~/src/lucky/lucky.go then you should be able to import "lucky" successfully.
更多推荐
发布评论