0

My GOROOT path :- C:\Go

I have set GOPATH to :- C:\Users\kunal\go

But when I import modules (like github.com/gorilla/mux) inside VS Code. VS Code prompts me this error :-

could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of C:\Go\src\github.com\gorilla\mux (from $GOROOT) C\src\github.com\gorilla\mux (from $GOPATH) \Users\kunal\go\src\github.com\gorilla\mux (from $GOPATH))

From above It is clear that it shows me two different GOPATH which I haven’t set. How do I fix this?

3 Answers3

2

I recommend going through the following path, using official documentation pages:

  1. Read about properly installing Go for your platform
  2. Read the getting started tutorial which also tells you how to install 3rd-party packages and use them in your code.

It should take you no more than 20 minutes to go through these steps, and it's almost certain that you'll be able to accomplish your goal by the end of the process. As a bonus, keep going through the Getting Started guide beyond the first page to learn how to create your own Go modules, use them from other modules, write tests, build your code into a binary, and more.

This is IMHO the minimal background required to even try writing Go programs; without going through these steps, you will lack crucial fundamental understanding and it will be hard to even understand SO answers.


Specifically in your case - please remember that at this time (with Go 1.16), GOPATH is pretty much deprecated and you should be using Go modules instead. The documents linked to above will explain this in detail.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • @truthadjustr: this answer suggests to stop using an old and deprecated way of doing things, and switch to the recommended and supported way - which has been recommended and supported for *years* now. – Eli Bendersky Jun 28 '22 at 15:46
  • What deprecated? Do you always want to download and re-download again and again the dependencies listed in `go.mod` ? The `GOPATH` is where these downloaded stuff are stashed. So, downloading every time from remote is the new way of doing things now? – daparic Jun 29 '22 at 06:41
  • @truthadjustr The dependencies in `go.mod` are only downloaded once and kept in a cache in `$HOME/go/pkg/mod`. You do not need to specify `GOPATH`, and it's recommended not to do so in modern versions of Go (for 2 years or so, by now). Reading the links I provided in the answer explains how things work now. Once you get a hang of it, the new module-based flow is far superior to the old `GOPATH` one - so it's a change for the best for 99.9% of developers – Eli Bendersky Jun 29 '22 at 12:30
  • True, that there are improvements here via the addition of `go.mod`. But in the overall scheme of things, it does not seem to make obsolete the utility of the `GOPATH` environment variable, as via this variable you can define to redirect where to locate your download cache. You may want to have it in drive D:/cache/ and only `GOPATH` can make that happen. – daparic Jun 30 '22 at 02:10
0

I ll give a TLDR for the solution given by @Eli Bendersky. If you don't understand GOPATH and go modules, you can look it up yourself here. Here I have assumed you are using VS Code with golang extension. I haven't tested it for other IDEs but it should work in a similiar way.

  1. In the source directory where you have the main.go, create a file named go.mod

  2. Name the package name to whatever you like and save the file.

  3. go to your terminal and run go build main.go, this will download all the missing packages(if any) and will update the go.mod file and create a new file go.sum to create the checksums of the package versions.

  4. All the package error squiggles should be gone by now in your IDE, if it doesn't try restarting your IDE once. You are good to go!

If you are stuck somewhere, let me know in the comments.

Shaswat Lenka
  • 544
  • 5
  • 11
-1

I just started to learn golang today. But it seems that I understood more from what these experts are trying to say when trying to answer this basic question.

I don't claim deep knowledge of golang. But as a 2100 rated chess player, I do claim common sense and rigorous thought process. The GOPATH environment variable is very much in use do not believe these kids. Only a kid would make such claims that GOPATH has no use.

The way I see it is that, the go.mod and go.sum plays in tandem with GOPATH.

Continue on your own journey, little tiger. I upvoted your question for moral support.

daparic
  • 3,794
  • 2
  • 36
  • 38
  • The tone used in your answer could be a bit more moderate and to the point. This discourages the person from asking questions. Please be polite in your replies. – Shaswat Lenka Sep 06 '22 at 06:22