导入Golang代码的本地文件夹(Import local folder of Golang code)

编程入门 行业动态 更新时间:2024-10-24 01:59:55
导入Golang代码的本地文件夹(Import local folder of Golang code)

我有一个'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.

更多推荐

本文发布于:2023-08-04 23:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   代码   Golang   Import   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!