go run/build not getting dependencies - go

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.

Related

Issue in installing a go package

So,I recently started following a video tutorial and i am fairly new to golang and tried installing the forked version of bolt db using
$ go get go.etcd.io/bbolt/...
Note : I want to use this specific version
but i am getting an error which says
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd#latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'
I read a few GitHub issues which say that go get is deprecated so how do I resolve this ?
I also tried few other things such as
go install go.etcd.io/bbolt/...
Go modules are today's standard. Especially if you are new to Go; do not spend time on material that do not use (and teach) them.
Run go mod init yourproject
in your project repository root directory. This will create go.mod file.
Once you have that you can either:
import go.etcd.io/bbolt in source code and then run go mod tidy. Go tool will find and add module to your dependencies (go.mod file). This is described in Getting started tutorial.
run go get go.etcd.io/bbolt directly, that will update dependencies too.
Using Go Modules series explains workflow in detail and will be helpful when converting commands from an outdated material.

Fix Go commands all giving "go: error loading module requirements"?

I am trying to install the dependencies for a Go project that uses Go modules. I have GO111MODULES=on in my environment.
No matter what command I run (go build, go get, go clean --modcache, ...) it fails with output something like:
sdgluck$ go build .
go: finding cloud.google.com/go v0.26.0
go: cloud.google.com/go#v0.26.0: unknown revision refs/tags/v0.26.0
go: error loading module requirements
The last line seems to appear in the output for every command, alongside some mention of cloud.google.com/go.
How can I fix this and just get Go to install the modules for this project?
As stated in a comment below, the proper command to clear the Go modules cache is go clean -modcache (just one dash).
If that still doesn't work then you can try manually clearing your Go mod folder.
For example, if your GOPATH was /Users/spongebob/go:
rm -rf /Users/spongebob/go/pkg/mod

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.

How to install "gotests" command?

I need to use the test driven development in Go using "gotests" command.
gotests -all *
This is not working. I did go get -u /github.com/cweill/gotests
and go install. But there is no binary created in $GOPATH/bin.
since there is NO main package, Use this command
$ go get github.com/cweill/gotests/...
this itself download all dependencies for the current package, and creates bin file, after downloading this package. see in $GOPATH/bin there will be a bin file named gotests
for more see HERE
The following worked for me with go v1.19.1
go install github.com/cweill/gotests/gotests#latest
Using go get to install things has been disabled since 1.18. See Deprecation of 'go get' for installing executables
go install github.com/rakyll/gotest
Source: https://github.com/rakyll/gotest

Resources