1

I'm in the process of relearning Go. I installed the latest Go version (1.7.1) using gvm and I am looking to build a simple rest api app using gin. I installed it using glide get https://github.com/gin-gonic/gin (glide) and that created a "vendor" folder on my project root. Running my app though, go run main.go, I encounter this error

main.go:3:8: cannot find package "github.com/gin-gonic/gin" in any of:
    /home/yowmamasita/.gvm/gos/go1.6.3/src/github.com/gin-gonic/gin (from $GOROOT)
    /home/yowmamasita/.gvm/pkgsets/go1.6.3/global/src/github.com/gin-gonic/gin (from $GOPATH)

It is not resolving the "vendor" directory glide just created

.
├── glide.lock
├── glide.yaml
├── main.go
├── README.md
└── vendor
    └── github.com
        └── gin-gonic
            └── gin

Not sure what's happening here, I thought after 1.5, it should be able to resolve imports from "vendor" directories without doing anything. I even added my projects folder on my $GOPATH

/home/yowmamasita/.gvm/pkgsets/go1.7.1/global:/home/yowmamasita/goprojects

What am I doing wrong here? I tried 1.6.3 too and I get the same error.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
yowmamasita
  • 4,810
  • 3
  • 17
  • 17
  • What is the output of `go env` and `go version`? – putu Oct 05 '16 at 03:29
  • @putu https://ghostbin.com/paste/ayebv – yowmamasita Oct 05 '16 at 03:35
  • The folder `/home/yowmamasita/goprojects` is not in your `GOPATH`. After modifying `GOPATH` variable, make sure to run `source .bashrc` or `source .bash_profile` or simply start a new terminal. – putu Oct 05 '16 at 03:44
  • @putu you mean this line doesn't mean anything `GOPATH="/home/yowmamasita/.gvm/pkgsets/go1.7.1/global:/home/yowmamasita/goprojects"`? – yowmamasita Oct 05 '16 at 03:46
  • It wasn't in the `ghostbin.com/paste/ayebv`. If you run `go run main.go` now, did you still get the error? – putu Oct 05 '16 at 03:51
  • @putu weird, I copy-pasted that from there. Anyway, yes, still getting the error – yowmamasita Oct 05 '16 at 03:59
  • Sorry, maybe I didn't read it correctly. [This issue](https://github.com/Masterminds/glide/issues/167) may help. In short, you should put your project root inside `$GOPATH/src/`. – putu Oct 05 '16 at 04:14
  • @putu can you please put this as an answer? This solved my problem. Thanks! – yowmamasita Oct 05 '16 at 07:57

1 Answers1

2

Please make sure:

  1. Add the workspace (/home/yowmamasita/goprojects) to $GOPATH variable.
  2. Typically under workspace there will be three directories which are bin, pkg and src. More details
  3. You can omit pkg and bin, but the project which is using vendor packages or your custom package must be placed under $GOPATH/src, otherwise go compiler will not recognized it.

More discussions can be found here and here

The structure should look like:

 $GOPATH
 └── src
     └── YOURPROJECT1
            ├── source codes #1
            └── vendor/

     └── YOURPROJECT2
            ├── source codes #2
            └── vendor/
putu
  • 6,218
  • 1
  • 21
  • 30