导入本地文件夹的Golang代码

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

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.

huangapple
  • 本文由 发表于 2014年3月28日 04:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/22698743.html
匿名

发表评论

匿名网友

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

确定