Cannot install vet package with go get - go

I'm running following command:
$ go get -u golang.org/x/tools/cmd/vet
package golang.org/x/tools/cmd/vet: cannot find package
"golang.org/x/tools/cmd/vet" in any of:
C:\Development\Software\go\src\golang.org\x\tools\cmd\vet (from $GOROOT)
C:\Development\Software\go\downloaded_packages\src\golang.org\x\tools\cmd\vet
(from $GOPATH)
I can't really understand how cannot find package makes sense with get. It is supposed to get the package from internet. Why is it looking for it locally?

#JimB summed it up in the comments - you don't need to install go vet separately.
But just for completeness, the direct answer to OP question is that the code for the go vet command no longer lives at golang.org/x/tools/cmd/vet (see https://golang.org/doc/go1.2#go_tools_godoc - and since that the code has moved to GitHub).
So when you run: go get -u golang.org/x/tools/cmd/vet it appears that it is git cloning the golang.org/x/tools/cmd package and then trying to compile golang.org/x/tools/cmd/vet which is resulting in an error ("cannot find package...") because the "vet" part doesn't exist - it moved out a while ago. (go get first downloads/clones the code and then attempts to compile the package on your local system.)
And all of that is to say, you probably already have go vet - try typing "go vet -h" and if it works (you should see something like: "usage: vet ..."), you're set.

Related

Cannot find package when running golang test

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...

go run/build not getting dependencies

go run and go build are not geting dependencies.
What I did:
I have done a go get of a package, it fetched it, and its dependencies, and build it. (all is good)
I run its command-line example program. (all is good)
I then created a new program based on this example, and go run it. (all is good)
Then copied this example program, and go run it. ( get a dependency error ).
Transcript
#↳ go version
go version go1.11.4 linux/amd64
#↳ echo $GOPATH
/home/????/+Files/workshops/programming/golang/gopath
#↳ go get -u github.com/cbroglie/mustache/...
#↳ cp -T $GOPATH/src/github.com/cbroglie/mustache/cmd/mustache/main.go my-mustache.go
#↳ go build -v my-mustache.go
my-mustache.go:8:2: cannot find package "github.com/spf13/cobra" in any of:
/usr/local/go/src/github.com/spf13/cobra (from $GOROOT)
/go/src/github.com/spf13/cobra (from $GOPATH)
I can see why it is not already installed: it was in a vendor sub-directory of the original source code. But why does it not install, when I build?
Check first your $GOPATH/bin folder: a go get -u github.com/cbroglie/mustache/... should already have compiled and installed all relevant binaries in it.
The README mentions:
To install mustache.go, simply run go get github.com/cbroglie/mustache/....
From the comments:
It looks like the mustache package is installed and working. However
when I try to build the cli example, it needs another package, if I go
get it then all is well, however I was expecting go build to install
all needed packages. Am I wrong?
go build itself won't install dependencies, so you need to go get it, or activate go 1.11 modules and declare that dependencies in your new program modules.

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! :)

Cannot build "chaintool/example02"

I am trying to follow instructions on Chaincode Development Environment to setup hyperledger in my local environment.
Unfortunately, I am completely new to golang. When I come across an error trying to build "chaintool/example02" I have no idea how to proceed further - should I ignore the issue or first I should fix something? For example, to run make with some options, etc ... How can I get missing imports ?
The output looks as follows:
hyper-00:chaincode hyper$ pwd
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/examples/chaincode/chaintool/example02/src/chaincode
hyper-00:chaincode hyper$ go build ./
chaincode_example02.go:24:2: cannot find package "hyperledger/cci/appinit" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/cci/appinit (vendor tree)
/usr/local/go/src/hyperledger/cci/appinit (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/cci/appinit (from $GOPATH)
chaincode_example02.go:25:2: cannot find package "hyperledger/cci/org/hyperledger/chaincode/example02" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/cci/org/hyperledger/chaincode/example02 (vendor tree)
/usr/local/go/src/hyperledger/cci/org/hyperledger/chaincode/example02 (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/cci/org/hyperledger/chaincode/example02 (from $GOPATH)
chaincode_example02.go:26:2: cannot find package "hyperledger/ccs" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/ccs (vendor tree)
/usr/local/go/src/hyperledger/ccs (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/ccs (from $GOPATH)
hyper-00:chaincode hyper$
My $GOPATH gives:
hyper-00:~ hyper$ echo $GOPATH
/Users/hyper/Projects/blockchain
[EDITED on 2017-01-02]
I repeated the repo cloning procedure again.
I think my mistake was I pulled a wrong source from GitHub - probably "master", not "v0.6".
What I did and the compilation works now is:
$ sudo apt install golang-go
$ gedit .profile
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/Projects/blockchain
$ . ~/.profile
$ mkdir -p $GOPATH/src/github.com/hyperledger/
$ cd $GOPATH/src/github.com/hyperledger
$ git clone -b v0.6 http://gerrit.hyperledger.org/r/fabric
$ cd ~/Projects/blockchain/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
$ go build
github.com/hyperledger/fabric should have included all these dependencies in its vendor/ folder or put the examples in their own repo with a vendor folder, so this looks like their problem not yours.
You can fix it by running go get x y z for all missing packages, or try using a vendoring tool to get them.
There are many options for vendoring but no "official" option until next year. I prefer govendor. i havent tried it with this particular repo but you can try with:
go get -u github.com/kardianos/govendor
cd /Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/
govendor fetch -v +missing
The HyperLedger team has provided very good documentation on how to use chaintool in a manner that allows users to document APIs (via Chain Code Interface, or CCI). Without this, consumers of a chaincode must inspect source code to understand how to compose a REST invocation. Great idea.
This approach to building chaincode requires you download github.com/hyperledger/cci and github.com/hyperledger/ccs to reside next to github.com/hyperledger/fabric within your $GOPATH. Unfortunately, I don't see any repository (on either github or gerrit) where this can be downloaded.
Further, I've only seen one example (thus far) of how to use chaintool with cci and ccs, which is chaintool/example02... and it does not compile due to these missing hyperledger packages.
There is documentation on chaintool with cci/ccs support at the following location:
https://github.com/hyperledger/fabric-chaintool
But it seems this documentation has been copied to the following location and renamed from 'chaintool' to 'openblockchain compiler' (or OBCC) and labeled as a 'work in progress':
https://libraries.io/github/hyperledger/fabric-chaintool
As such, I am inclined to believe the chaintool/example02 is not a currently supported approach to building chaincode... at least until OBCC becomes officially available.

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

Resources