golang cannot add a package

60 views Asked by At

I am trying to add the godotenv package to my working project.

I run

go get github.com/joho/godotenv

from the command line. This inserts the dependency into my go.mod file, which now looks like this:

...
github.com/joho/godotenv v1.5.1
...

VScode flags this as an error and says

github.com/joho/godotenv is not used in this module

and asks me to run go mod tidy. But if I do that, it removes the dependency from go.mod

Alternatively, I add the import

"github.com/joho/godotenv"

to the module where I want to use it (main.go)

VScode then reports an error

could not import github.com/joho/godotenv (no required module provides package "github.com/joho/godotenv")compilerBrokenImport

and as a quick fix advises me to run

go get github.com/joho/godotenv

However, if I do this, the same error appears in go.mod.

And if I save the main.go file, the import is automatically removed. So this seems to be going round in circles.

Further information: I have a vendors folder, which is required by heroku where I am currently hosting the project. I tried running

go mod vendor

at each of the points in the above cycle, but the problem persists.

What sequence of actions will install this package for me?

1

There are 1 answers

1
shosae On

It's ever happen to me. I use function in code and import it then go mody tidy && go mod vendor

try use it once in your code.

godotenv.Load()

and import it and run go mody tidy && go mod vendor again.