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.
Related
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.
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! :)
I am using go 1.5.1 on Windows 8.1 64-bit. I do not have GO15VENDOREXPERIMENT set in my environment. I have the latest version of git and bazaar installed.
I am trying to get the gomniauth package:
go get github.com/stretchr/gomniauth
Even though the process completes without any error, a lot of dependencies aren't pulled in.
For example, when compiling my app (which depends on gomniauth), I get these errors:
..\github.com\stretchr\codecs\xml\simple_xml_codec.go:5:2: cannot find package "github.com/clbanning/x2j" in any of:
C:\Go\src\github.com\clbanning\x2j (from $GOROOT)
C:\work\src\github.com\clbanning\x2j (from $GOPATH)
..\github.com\stretchr\codecs\msgpack\msgpack_codec.go:6:2: cannot find package "github.com/ugorji/go/codec" in any of:
C:\Go\src\github.com\ugorji\go\codec (from $GOROOT)
C:\work\src\github.com\ugorji\go\codec (from $GOPATH)
..\github.com\stretchr\codecs\bson\bson_codec.go:5:2: cannot find package "labix.org/v2/mgo/bson" in any of:
C:\Go\src\labix.org\v2\mgo\bson (from $GOROOT)
C:\work\src\labix.org\v2\mgo\bson (from $GOPATH)
It seems to pull in the direct dependencies for gomniauth, but doesn't pull in the dependencies of the dependencies. I have gone and deleted the stretchr folder from my GOPATH/src as well as GOPATH/pkg, but after running go get many times, it is still not pulling in the any dependencies beyond the second level.
I am 100% confident there are no network issues on my end. I can access those github repos using my browser or curl.
Change directory to your project and then try go get ./...
E.g.:
cd C:\work\src\github.com\stretchr\gomniauth
go get ./...
Or just go get github.com/stretchr/gomniauth/... as Amit Kumar Gupta suggested
In my case I was missing the bzr package.
After adding it using dnf install bzr and running #RoninDev suggestion it worked as expected:
cd $GOPATH/src/github.com/stretchr/gomniauth
go get ./...
I'm new to Go and I'm trying to set up a Go project with minimal documentation: https://github.com/alphagov/metadata-api
I've cloned it, but when I try go build I get the following warnings:
main.go:8:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus (from $GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus (from $GOPATH)
main.go:14:2: cannot find package "github.com/alphagov/metadata-api/content_api" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api (from $GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api (from $GOPATH)
I'm guessing this is because I haven't installed the Go equivalent of requirements?
My GOPATH is set:
metadata-api$ echo $GOPATH
/Users/me/go
And the Go executable is in
metadata-ape$ echo $PATH
....:/Users/me/go/bin
What do I need to do to help Go find these packages?
You should install package first:
try
$ go get github.com/Sirupsen/logrus
and check you $GOPATH dir
This project use gom as the package manager,
Make sure you have installed gom
or try this command
$ gom install
I think your $GOPATH and $PATH settings are incorrect, the $GOPATH environment variable specifies the location of your workspace, these are my path settings:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
I had similar issue and
export GO111MODULE=on
helped.
When you need your code to do something that might have been implemented by someone else (in Github or a package somewhere else), You should initialize a go mod file inside of your folder.)
For the purposes of this example, I'll just use example.com/module.
go mod init example.com/module
Add new module requirements and sums:
go mod tidy
Run your program:
go run .
For more details, see https://golang.org/doc/tutorial/getting-started.
Was able to fix the similar issue in Go 1.13.7 by typing:
export GOPATH=~/go
go get github.com/profile/repository
(e.g. github.com/Sirupsen/logrus)
"...Starting in Go 1.13, module mode will be the default for all development..."
"...When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin)..."
I'm using GVM to manage my go installations and paths and everything seems to work just fine - I can run tests and produce builds. I'm now trying to produce a code coverage file and am having some difficulty.
My package is defined in /home/bill/dev/server/model.
When I run:
$ go test -cover -coverprofile cover.out
The tests run successfully and a coverage file is produced. However, the paths in the coverage file look like this:
_/home/bill/dev/server/model/activity.go:19.34,21.2 1 1
And I get the following error when I try to create an html cover file:
$ go tool cover -html=cover.out
cover: can't find "activity.go": cannot find package "_/home/bill/dev/server/model/" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/_/home/bill/dev/server/model (from $GOROOT)
/home/bill/.gvm/pkgsets/go1.2/global/src/_/home/bill/dev/server/model (from $GOPATH)
How do I fix this?
Additional details
~ gvm use go1.2
Now using version go1.2
~ echo $GOPATH
/home/bill/.gvm/pkgsets/go1.2/global
~ echo $GOROOT
/home/bill/.gvm/gos/go1.2
I tried manually setting my $GOPATH but that didn't change the cover.out file. I also tried manually editing the cover.out file but I can't figure out what paths it actually wants. In the default configuration shown above, running go test runs as expected.
Attempting to fix GOPATH
~ export GOPATH=/home/bill/dev/
~ ln -s /home/bill/dev/server /home/bill/.gvm/gos/go1.2/src
~ go test
cannot find package "code.google.com/p/go.crypto/pbkdf2" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/code.google.com/p/go.crypto/pbkdf2 (from $GOROOT)
/home/bill/dev/src/code.google.com/p/go.crypto/pbkdf2 (from $GOPATH)
../util/log.go:4:2: cannot find package "github.com/kr/pretty" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/github.com/kr/pretty (from $GOROOT)
/home/bill/dev/src/github.com/kr/pretty (from $GOPATH)
These are additional dependencies that I previously downloaded using go get. They end up in /home/bill/.gvm/pkgsets/go1.2/global/src which the $GOPATH used to point to. So I changed GOPATH
~ export GOPATH=/home/bill/dev/:/home/bill/.gvm/pkgsets/go1.2/global
So that the tests run again, but the cover.out file still has the same directories in it and still gives me the same error.
Here's the way to get all of the advantages of GVM without having to ruin your ideal go development environment as described here, and without having to resort to clunky special-case symlink hacks.
Suppose I've set all of my development up according to the standard in ~/go (so package foo in my github would be in ~/go/github.com/gepoch/foo)
First of all, we're going to make a special-use pkgset that will happily reference our development environment. Simply run:
$ gvm pkgset create dev
This will add the pkgset. Next, we can do some customization on where exactly it puts the go path. Run:
$ gvm pkgenv dev
You should see your favorite text editor pop open with a bunch of environment variable definitions. Simply change the GOPATH entry to include your dev root! For example, I change this:
export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev"
Into this:
export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev:$HOME/go"
Additionally, have gvm set up your path correctly by changing this:
export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"
into this:
export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}:$HOME/go/bin"
Restart your terminal, and that's it! Whenever you run $ gvm pkgset use dev you'll have easy access to your dev environment.
That means (among many other things) that this works as intended:
$ go test -coverprofile=coverage.out github.com/gepoch/foo
$ go tool cover -html=coverage.out
You can add this to any pkgset environment that you wish, for easy access to the dev tree.
I had the same problem a month ago. I solved it by using the following steps.
My package name is called alpaca
My working directory (code) is /home/pksunkara/coding/alpaca
$ gvm use go1.2
Now using version go1.2
$ echo $GOPATH
/usr/local/lib/gvm/pkgsets/go1.2/global
$ echo $GOROOT
/usr/local/lib/gvm/gos/go1.2
To fix the issue, I did this
$ mkdir -p $GOPATH/src/github.com/pksunkara
$ ln -s /home/pksunkara/coding/alpaca $GOPATH/src/github.com/pksunkara/alpaca
Basically I have to link the current working folder into the $GOPATH/src folder and the resultant package path for alpaca became github.com/pksunkara/alpaca.
Now, the go test & cover works as following
$ go test -coverprofile=coverage.out github.com/pksunkara/alpaca
$ go tool cover -html=coverage.out
THIS IS IMPORTANT
I stumbled a lot to fix this. I have attempted all kind of things including the ones you attempted. I understood the problem by reading about code organization in golang which should be a must read for everyone working with go.
The code organization mentioned here is very important to work with golang.
Package paths are important for golang. And you should never use local path when importing in golang. They will work but it is not recommended.
Let's assume your package name is model. You can simply link the model directory to $GOPATH/src/model and then you will have a package path named model which you can import using import "model". But to avoid collisions, go recommends using a bigger package path name.
I would recommend you to link it to $GOPATH/src/bill.com/server/model and import it as import "bill.com/server/model". Similarily with ./query and ./util you have.
If you still have doubts, please ask. I will try to explain more.
Have you try to put a issue in gvm's developer site? https://github.com/moovweb/gvm (I'm not sure is this the major site)
Double check the value of $GOPATH, as set by gvm.
I would try setting $GOPATH manually just for testing to /home/bill (with a symlink src->dev), just to see if a go test -cover produces files a cover.out with the correct file path in it.