94

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?

Richard
  • 62,943
  • 126
  • 334
  • 542
  • Update: I added Go to my path `export PATH=$PATH:/Users/anna/go` and now `go build` does not produce any errors... but nor does it seem to generate anything. – Richard Dec 02 '14 at 12:25

5 Answers5

102

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
simeg
  • 1,889
  • 2
  • 26
  • 34
lidashuang
  • 1,975
  • 4
  • 20
  • 22
  • 1
    Thanks. `gom install` seems to be doing the trick, but how did you know this project used `gom`? – Richard Dec 02 '14 at 12:33
  • 14
    I also tried simly `go get` and I think it got all of my dependencies at the same time. – John B Apr 03 '17 at 14:49
  • $GOPATH environment variable specifies the location of your workspace? Does that mean i have to export all of these variables everytime i create a new workspace-project? – Ugur Yilmaz Apr 22 '20 at 14:17
  • This is the closest to the answer and provides the correct directions to reach the fix, other answers should not be approached. But it is important to specify, why we are doing this. - GO has a concept of WORKSPACE, so whenever we do `go build` , the go compiler will search for the two `environment` variables, `GOPATH` and `GOROOT` to find the `src` path to compile the package and generate the binaries - So do we always need to have a `WORKSPACE` set, yes. Unless.... you would want to stuff everything (meaning every package) in the `GOPATH` or read - https://go.dev/doc/gopath_code – Sandeep Anand Dec 29 '21 at 17:09
  • The `gom` project is now archived. – Saikat Jan 29 '22 at 15:40
48

I had similar issue and

export GO111MODULE=on 

helped.

rgaut
  • 3,159
  • 5
  • 25
  • 29
5

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Dilshan Dilip
  • 707
  • 6
  • 9
3

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)
monkrus
  • 1,470
  • 24
  • 23