英文:
How do I install requirements in Go? "cannot find package"
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是Go的新手,正在尝试使用最少的文档设置一个Go项目:https://github.com/alphagov/metadata-api
我已经克隆了它,但是当我尝试运行go build
时,我收到以下警告:
main.go:8:2: 无法在以下任何位置找到包“github.com/Sirupsen/logrus”:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus(来自$GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus(来自$GOPATH)
main.go:14:2: 无法在以下任何位置找到包“github.com/alphagov/metadata-api/content_api”:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api(来自$GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api(来自$GOPATH)
我猜这是因为我没有安装Go的依赖包的原因?
我的GOPATH
已经设置好了:
metadata-api$ echo $GOPATH
/Users/me/go
而Go可执行文件在以下位置:
metadata-ape$ echo $PATH
....:/Users/me/go/bin
我需要做什么来帮助Go找到这些包?
英文:
I'm new to Go and I'm trying to set up a Go project with minimal documentation: https://github.com/alphagov/metadata-api
I've cloned it, but when I try go build
I get the following warnings:
main.go:8:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus (from $GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus (from $GOPATH)
main.go:14:2: cannot find package "github.com/alphagov/metadata-api/content_api" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api (from $GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api (from $GOPATH)
I'm guessing this is because I haven't installed the Go equivalent of requirements?
My GOPATH
is set:
metadata-api$ echo $GOPATH
/Users/me/go
And the Go executable is in
metadata-ape$ echo $PATH
....:/Users/me/go/bin
What do I need to do to help Go find these packages?
答案1
得分: 102
你应该先安装包:
尝试运行以下命令:
$ go get github.com/Sirupsen/logrus
并检查你的 $GOPATH
目录。
这个项目使用 gom
作为包管理器,
确保你已经安装了 gom
,
或者尝试运行以下命令:
$ gom install
我认为你的 $GOPATH
和 $PATH
设置不正确,$GOPATH
环境变量指定了你的工作空间的位置,以下是我的路径设置:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
英文:
You should install package first:
try
$ go get github.com/Sirupsen/logrus
and check you $GOPATH
dir
This project use gom
as the package manager,
Make sure you have installed gom
or try this command
$ gom install
I think your $GOPATH
and $PATH
settings are incorrect, the $GOPATH
environment variable specifies the location of your workspace, these are my path settings:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
答案2
得分: 48
我遇到了类似的问题,然后使用了以下命令来解决:
export GO111MODULE=on
英文:
I had similar issue and
export GO111MODULE=on
helped.
答案3
得分: 5
当你需要让你的代码执行一些可能已经由其他人实现的功能(在Github或其他地方的软件包中),你应该在你的文件夹中初始化一个go mod文件。
以本例为例,我将使用example.com/module。
go mod init example.com/module
添加新的模块要求和摘要:
go mod tidy
运行你的程序:
go run .
更多详情,请参阅https://golang.org/doc/tutorial/getting-started。
英文:
When you need your code to do something that might have been implemented by someone else (in Github or a package somewhere else), You should initialize a go mod file inside of your folder.)
For the purposes of this example, I'll just use example.com/module.
go mod init example.com/module
Add new module requirements and sums:
go mod tidy
Run your program:
go run .
For more details, see https://golang.org/doc/tutorial/getting-started.
答案4
得分: 3
我能够通过以下方式解决类似问题:
export GOPATH=~/go
go get github.com/profile/repository
(例如:github.com/Sirupsen/logrus)
英文:
Was able to fix the similar issue in Go 1.13.7 by typing:
export GOPATH=~/go
go get github.com/profile/repository
(e.g. github.com/Sirupsen/logrus)
答案5
得分: 0
从Go 1.13开始,模块模式将成为所有开发的默认模式。
在使用模块时,不再使用GOPATH来解析导入。然而,它仍然用于存储已下载的源代码(在GOPATH/pkg/mod中)和已编译的命令(在GOPATH/bin中)。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论