Cannot find package when running golang test - go

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.

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

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

Related

How to config a simple Go project

I'm trying to follow the Writing an intepreter in Go book, by Thorsten Ball, and in the first chapter he establish this simple scheme
file /Users/myuser/projects/monkey/token/token.go
file /Users/myuser/projects/monkey/lexer/lexer.go
file /Users/myuser/projects/monkey/lexer/lexer_test.go
In lexer/lexer.go and lexer/lexer_test.g the files start as
package lexer
And in lexer_test.go the imports are
import (
"testing"
"monkey/token"
)
Then he says that for running the test, I have to run (from /Users/myuser/projects/monkey directory):
go test lexer/lexer_test.go
But when I do this, I receive the error:
lexer/lexer_test.go:6:2: cannot find package "monkey/token" in any of:
/usr/local/opt/go/libexec/src/monkey/token (from $GOROOT)
/Users/myuser/golang/src/monkey/token (from $GOPATH)
FAIL command-line-arguments [setup failed]
FAIL
I've been trying to understand how to configure the packages in go, but I found a lot of very complicated documentation about GOPATH, GOROOT and go.mod. I've been trying all this approach without get rid of the issue.
Can someone help me please? I'm sure is a simple fix but I cannot figure it out :(
As the error message says, the compiler couldn't find the package locally.
Are you sure you have installed the package?
You may need to do go get [packagename]
For e.g., go get golang.org/x/tools/cmd/goimports
For Golang, It will find package first from go root then go path then import from outside. So basically, you should have that package monkey/token in your go root or go path.
I don't think it is about goimport because monkey/token didn't seem to be official lib that can be import.
When I first try Golang I have the same problem. I solve by create that package in go path (your working dir) or create that package from your book.

ide does not find packages shim and proto packages in $GOPATH

I have been setting up a small Hyperledger fabric application. It is already running and I can add delete and change users.
But until now I have been using nano to code until now.
I want to change to a proper ide (goland) for the sake of autocompletion and so on.
The problem is: On my local machine it can not find the packages
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
The gopath is set up correctly but the two packages are not in the folders. I.e also a local go build does not work.
The packages are present on the CLI though.
How can I install the dependencies so that I also have them on my local machine? And is this even desired and if so why not?
ps: I have already tried
go get -u github.com/hyperledger/fabric/core/chaincode/shim
the resulting error is:
package github.com/hyperledger/fabric/core/chaincode/shim: cannot find package "github.com/hyperledger/fabric/core/chaincode/shim" in any of:
/usr/local/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOROOT)
/home/funuser/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOPATH)
The issue is, the shim and protos were moved to stand-alone repositories several months ago, you need to do a go get on github.com/hyperledger/fabric-chaincode-go/shim and github.com/hyperledger/fabric-protos-go and change your import references to these new repos:
import (
"fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
Unfortunately, go get only imports go code files. Other assets like the raw proto files are excluded. An option is to check-out the git repo in $GOPATH/src/github.com/hyperledger/fabric
this worked for me https://www.programmersought.com/article/17446289383/
additional any one using this thread now need to update the pkgs to
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"

How to build Golang blog from github?

Im not very good with Go and I am having a lot of problems with understanding how common website features are made, so I thought it would be good to see a real example. I tried building https://github.com/golang/blog but its not working.
My gopath is apparently C:/Users/me/go as it should be.
*Edit Except if I run cd $GOPATH/src, it says C:\src doesnt exist, it looks in C: not C:/Users
Method 1. (running go get -u golang.org/x/blog)
I open Powershell and run that in my Users/me/go/src directory and it says:
can't load package: package golang.org: no Go files in
C:\Users\me\go\src\golang.org
But it does download the source files. So its basically this step?
'u can manually git clone the repository to $GOPATH/src/golang.org/x/blog.'
Then I dont know where to run go build or what to run. I tried
go build -o blog.exe ./blog
and it says
can't load package: package blog: cannot find package "blog" in any of:
C:\Go\src\blog (from $GOROOT)
C:\Users\me\go\src\blog (from $GOPATH)
I tried running the same command in different directories of the project and doesnt work.
I'll try to answer your questions. (Note that I am a Linux user, so there may be some discrepancies with the Windows commands below. You may want to follow these directions: http://www.wadewegner.com/2014/12/easy-go-programming-setup-for-windows/ to setup the GOROOT environment variable.)
For method 1, the -u flag tells go to update the source code. Since you haven't downloaded it before, it lets you know with the error you see. What you want to run is go get golang.org/x/blog.
To build the package, you first want to change the directory (cd) to the package root, so cd %GOPATH%\src\golang.org\x\blog or cd C:\Users\me\go\src\golang.org\x\blog. Next, you want to run go build. Then, you can run the output file, which should automatically be named blog.exe.
Hopefully this helps! :)

Build a GO program in Windows

I have found an interesting program in the language of Go. This is the first time I work with this language and I dont know much of it.
Basically it is a network bandwidth speedtest.
The program works verry well, I just would like to modify its output a little. So I have to edit the source and rebuild the project to get the update .exe file.
The wiki of the project says:
Building speedtest
Clone/build speedtest go get github.com/zpeters/speedtest
cd ~/go/bin/code>
./speedtest
I did step 1. but I don't know what to do with step 2. ? What is it ? What is "~" means and what is "code>" ?
Also is "./" means running a shell script file? I work in Windows. Can somebody please help me to compile this Go program?
E D I T:
The src contains 3 packages:
github.com
golang.org
gopkg.in
How should I compile?
If I try:
go install speedtest
I got
can't load package: package speedtest: cannot find package "speedtest" in any of:
C:\go\src\speedtest (from $GOROOT)
C:\gonetwork\src\speedtest (from $GOPATH)
Your message line:
C:\gonetwork\src\speedtest (from $GOPATH)
suggests that your GOPATH is set to c:\Gonetwork.
Do it like this then, open a command prompt.
c:
cd \GoNetwork\src
go get github.com/zpeters/speedtest
When it completes:
c:\GoNetwork\bin\speedtest.exe
As far as you have been able to execute go get github.com/zpeters/speedtest with no error, you already have your code in your $GOPATH and the binary in $GOBIN, the only thing you need to modify it is go to its location, in your case, according to your output it should be something like:
cd C:\gonetwork\src\speedtest\github.com\zpeters\speedtest
Once you are there, and modified the code to your needs, you only need to run go install github.com/zpeters/speedtest

Unable to use Go get properly

I am a new user to go and I am trying this command.
go get github.com/tensorflow/tensorflow/tensorflow/contrib/go
And I am getting this error
package github.com/tensorflow/tensorflow/tensorflow/contrib/go
imports github.com/tensorflow/tensorflow/tensorflow/contrib/go
imports github.com/tensorflow/tensorflow/tensorflow/contrib/go: cannot find package "github.com/tensorflow/tensorflow/tensorflow/contrib/go" in any of:
/usr/lib/go/src/pkg/github.com/tensorflow/tensorflow/tensorflow/contrib/go (from $GOROOT)
/home/arafat/go/src/github.com/tensorflow/tensorflow/tensorflow/contrib/go (from $GOPATH)
I know this seems to be a trivial issue but I am stuck at it.
If the code to compile and install is not in the master branch (checked out by default by the go get), but only in the go branch of that repo, try and:
cd $GOPATH/github.com/tensorflow/tensorflow
git checkout go
Then try again the compilation.

Resources