英文:
Import local folder of Golang code
问题
我有一个名为'example.go'的文件,我试图导入同一文件夹中的一个目录。
我有'example.go'和'project'文件夹中的'lucky'目录。
这是我尝试导入'lucky'目录的方式:
import (
"fmt"
golucky "goLucky"
"io/ioutil"
"os"
)
但是当我运行example.go时,它似乎试图从go源代码中导入它,因为它抛出以下错误:
在任何位置都找不到包"goLucky":
/usr/local/go/src/pkg/goLucky(来自$GOROOT)
($GOPATH未设置)
如何导入与文件相同目录中的本地文件夹?
英文:
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?
答案1
得分: 1
你需要设置你的GOPATH
环境变量,并将你的lucky
目录放在其中。参考链接:http://golang.org/doc/code.html#Organization
举个例子,如果你设置了GOPATH=~
,那么将你的lucky.go
文件放在~/src/lucky/lucky.go
中,你就可以成功地导入"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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论