Using Go 1.2 cover tool with GVM - go

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.

Related

How to find a Go package installed with 'go get -u' when no $GOPATH is defined?

I'm following the gRPC Quickstart tutorial for Go, https://grpc.io/docs/quickstart/go/, and have installed gRPC using the command
go get -u google.golang.org/grpc
I actually haven't defined a GOPATH environment variable:
> echo $GOPATH
which, as I understand it, means that it defaults to ~/go, or in my case /Users/kurt/go.
At the next step, I'd like to build the example by doing
cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
However, I find that the directory doesn't exist, and there is also no google.golang.org directory in /Users/kurt/go/src:
~/g/src> ls *google*
fish: No matches for wildcard '*google*'. See `help expand`.
ls *google*
^
Should the package not be located here? That's what I understand from Where does go get install packages?.
Using Go Modules, you can find 'go get' downloaded files at:
~/go/pkg/mod/cache/download
However, it should be treated like an immutable copy of the source code.
If you want a mutable copy of the source code, you should clone the repository:
git clone https://github.com/grpc/grpc-go
In your example output, you are in ~/g/arc
Go path default would be ~/go/src
I think auto complete bit ya there
In the end, I worked around the problem by cloning https://github.com/grpc/grpc-go which appears to contain the examples/helloworld directory I'm looking for. Still curious to hear where the package downloaded with go get is located.

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.

Golang equivalent of npm install -g

If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs
npm install -g express
Installs express such that I can run the command
express myapp
and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it
Thanks in advance!
Update: If you're using Go 1.16, this answer still works, but go install has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on in your environment, go get will not install packages "globally". It will add them to your project's go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get normally to install binaries to $GOPATH/bin.
There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.
Starting with Go >= 1.16 the recommended way to install an executable is to use
go install package#version
For example, go install github.com/fatih/gomodifytags#latest.
Executables (main packages) are installed to the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set. You need to add this directory to your PATH variable to run executables globally. In my case, I've added this line to my ~/.zshrc file. (if you are using bash, add it to the ~/.bash_profile file):
export PATH="$HOME/go/bin:$PATH"
Go team published a blog post about this change, here's the explanation quote:
We used to recommend go get -u program to install an executable, but this use caused too much confusion with the meaning of go get for adding or changing module version requirements in go.mod.
Refer to go install documentation for more details
As far as I know, there is no direct equivalent to npm install -g. The closest equivalent would not be go install, but go get. From the help page (go help get):
usage: go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
By default, go get installs binaries to $GOPATH/bin, so the easiest way to make those binaries callable from everywhere is to add that directory to your $PATH.
For this, put the following line into your .bashrc (or .zshrc, depending on which shell you're using):
export PATH="$PATH:$GOPATH/bin"
Alternatively, you could also copy or link the executables to /usr/local/bin:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
Short solution for Linux users:
Use the go get command as usual
Add the following lines to .bashrc:
# This is the default GOPATH, you should confirm with the 'go env' command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Restart terminal or source it. Installed binaries will be available globally.
For Go v1.8+
go install package_name#latest
Caveat: this answer is outdated following the 2020 deprecation of go get. The solution presented here won't work with newer Go runtime installs.
The closest analogue of this in Go would be go get. By default, it will fetch a Go package from a supplied repository URL, and requires a $GOPATH variable to be set in your shell so that Go knows where to store the packages (and subsequently where to find them when compiling code depending on go get-ted packages).
Example syntax:
$ go get github.com/user/repo
The behaviour supplied by npm's -g flag is default, and packages installed using go get are normally available globally.
See go get --help for more information about the command.
As mentioned by #helmbert, adding your $GOPATH to your $PATH is useful if you're installing standalone packages.
if you are using zsh :
first: install your package using :
go install package#version
then , you edit your .zshrc file
nano ~/.zshrc
Add this line to the end of .zshrc file :
export PATH="$HOME/go/bin:$PATH"
last but not least :
source ~/.zshrc
then open a new terminal and execute your command :)
TL;DR at the bottom. I'm going to walk you through how I came to this conclusion, and why the more obvious solutions don't work.
Upon seeing this question, I thought "If I could set root's GOPATH=/usr, it would install things in /usr/bin/ and /usr/src!"
So I tried the obvious thing:
Add GOPATH=/usr to root's .bashrc.
And it worked!
Sort of.
Not really.
Turns out, sudo doesn't execute root's .bashrc. For "security" or something like that.
Do env_set or something in /etc/sudoers
Turns out, /etc/sudoers can only remove environment variables. There's no env_set directive.
(As far as I can find)
Dig through man sudoers.
Where does sudo get it's default set of environment variables from?
Well, the first one in the list is /etc/environment, so that's the one I used.
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get <repo>
Binaries will be put in /usr/bin, and sources will be put in /usr/src.
Running go as non-root will use GOPATH the "normal" way.
If you don't have go installed, you may use the gobinaries. it builds an on-demand binary of the project from github repo.
The command to install the go package would be:
curl -sf https://gobinaries.com/rakyll/hey | sh

How do I install requirements in Go? "cannot find package"

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

Resources