Home > Mobile >  Cannot find package when running golang test
Cannot find package when running golang test

Time:03-10

When I run go tests I get the error:

cannot find package "github.com/stretchr/testify/assert" in any of:
    /usr/local/Cellar/go/1.17.6/libexec/src/github.com/stretchr/testify/assert (from $GOROOT)
    /Users/MyName/go/src/github.com/stretchr/testify/assert (from $GOPATH)

I do however see that my local installation of stretchr and other tools ARE in /Users/MyName/golang/pkg/mod/github.com

How do I get golang to look in the right place for these installations? I thought it would be grabbing the resource from github if the import is pointing to the repo.

Are my installations in the "wrong" spot, and should I move them to where golang would look for them by default?

I have been able to test using stretchr before, but upgrading the package didn't change anything.

Running go get github.com/stretchr/testify and go mod tidy also didn't seem to change the behavior.

Thanks in advance for your help.

CodePudding user response:

Run "go mod init" whilst in your folder with your code, then go mod tidy. It should work, if not, try go get ... then try this again. Some IDE's such as VSCode delete imports when saving if they're unrecognised, or not used, so make sure when you run go mod init, and tidy, that the import is actually in the file

CodePudding user response:

I closed VS Code and re-opened, and it's working now...

  • Related