如何在 Go 中安装需求? “无法找到软件包”

我是 Go 的新手,我试图用最少的文档建立一个 Go 项目: https://github.com/alphagov/metadata-api

我已经克隆了它,但是当我尝试 go build时,我会得到以下警告:

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)

我猜这是因为我没有安装相当于需求的 Go?

我的 GOPATH设置好了:

metadata-api$ echo $GOPATH
/Users/me/go

Go 可执行文件在

metadata-ape$ echo $PATH
....:/Users/me/go/bin

我要怎么做才能帮你找到这些包裹?

123176 次浏览

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

I had similar issue and

export GO111MODULE=on

helped.

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)

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.