How to install Go package manually from source code - go

I'm living in China and is not able to download & install GO package through command (event I use the vpn network):
go get -u <repo_url>
but I can access the repo_url and downloand its source code.
So my question is can I put the scource code under src folder and run commamd ? :
go install
if yes, what's the different betweeen the two way ?

for example, you have the repo_url at https://github.com/hello/example
You can do go get manually by
$ cd $GOPATH
$ mkdir -p src/github.com/hello
$ cd src/github.com/hello
$ git clone https://github.com/hello/example.git
$ cd example
$ go install
the binary will install into $GOPATH/bin
if the go program of the repo_url depends on other go package. you have to manually get it and put it to correct path location too.

Two things are important when one looks to get a required package manually,
Path to the package repository; for example, github.com/golang/crypto.git package is hosted on Github. However, when using on code should import as golang.org/x/crypto/bcrypt
Google the required package
Path in the $GOPATH. In this example, the repository should be cloned inside golang.org directory and inside the appropriate directories. To me, the solution to find the path is running code and read the errors for missing modules/packages.

Related

go install #latest found but does not contain package

I'm trying to install my package using go install but I get this error message when running the command go install github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest:
go install: github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest: module github.com/JoaoDanielRufino/gcloc#latest found (v1.0.0), but does not contain package github.com/JoaoDanielRufino/gcloc/cmd/gcloc
I want the executable name to be gcloc.
Here is the current source code: https://github.com/JoaoDanielRufino/gcloc
Note: I've already tried go clean -modcache but it didn't work
As the main function of this package isn't on its root, you should pass the directory of the main package on your command.
So, your command will be:
go install -v github.com/JoaoDanielRufino/gcloc/cmd#latest
I came across a similar issue when I was trying to use go install to install the cloudflare/cf-terraforming tool on my machine. The documentation for this tool is not clear on the installation and I had to dig around to get this to work
Basically #Jictyvoo answer above sums it up, if the path is pointing to anything other than directory where the main.go file is sitting I got the error
Command: go install github.com/cloudflare/cf-terraforming#latest v0.8.0#latest
go: github.com/cloudflare/cf-terraforming#latest: module
github.com/cloudflare/cf-terraforming#latest found (v0.8.0), but does not
contain package github.com/cloudflare/cf-terraforming
when I switched to the below it worked fine for me:
Command: go install -v github.com/cloudflare/cf-terraforming/cmd/cf-terraforming#latest
This worked for me after checking the repo and realising that the main.go file was sitting in the cmd/cf-terraforming subdirectory

Installing gota package in go workspace

I'm writing this away from my code so fingers crossed.
I've recently started learning Go from a Python background. I've set up my workspace (Linux Mint OS) so:
GOPATH=$HOME/go
GOROOT=/usr/local/go
Where under $HOME i have a dir called go and 3 subdirs called src, bin and pkg.
I wanted to mess around with some dataframes (I use pandas a lot in Python) so I tried to install gota from github. Only their recommended install command:
go get -u github.com/kniren/gota/dataframe
go get -u github.com/kniren/gota/series
returns an error saying it could find the package in GOPATH or GOROOT. To me this is strange as go get seems like the equivalent to pip install and shouldn't be looking in my path but rather at the URL. I managed to get some files to install but using goget and the github URL of the project:
go get github.com/go-gota/gota/dataframe
go get github.com/go-gota/gota/series
and this built some files under a gonum.org directory in my src dir and a linux_amd64 dir in my pkg directory. So far neither section have the expected path to the libraries and I can't find a suitable method to import. import statements look in gopath's src directory however I assume it should be looking in the pkg directory? Why is this and what's wrong with my env?
The authors of the repository must have migrated to a different repository.
The official repository of these packages is: github.com/go-gota/gota
https://www.github.com/kniren/gota/dataframe
https://www.github.com/kniren/gota/series
These repositories do not exist, this is why your go get commands failed. In fact, trying to navigate to github.com/kniren/gota redirects me to their official repository.

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.

Using Go 1.2 cover tool with GVM

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.

How to place Golang project (a set of packages) to Github?

It is not clear for me from Golang tutorial how to put Golang code to Github to be able to import that code as a package from Github later.
This is an example project-workspace (directory structure) from the Golang tutorial http://golang.org/doc/code.html:
bin/
hello # command executable
pkg/
linux_amd64/ # this will reflect your OS and architecture
github.com/user/
newmath.a # package object
src/
github.com/user/
hello/
hello.go # command source
newmath/
sqrt.go # package source
So, what do I need to do, where do I need to git init in this workspace, to be able later:
To import only newmath package into my some separate project. This way:
import "github.com/user/newmath"
To get only hello.exe executable.
To get the whole project-workspace (all directories: bin, pkg, src).
For the package newmath it's the same as (later 2.)
$ mkdir $GOPATH/src/github.com/username/newmath
$ cd $GOPATH/src/github.com/username/newmath
$ git init
$ ... more git setup
$ touch sqrt.go
$ gvim sqrt.go
$ git add sqrt.go
$ git commit -a -m 'Inital commit'
$ git push
Now people can do
$ go get github.com/username/newmath
and
import "github.com/username/newmath"
should now work in their sources. The package will be installed on
demand automatically.
I'll assume that the hello command and the newmath package are
not related, or not enough tightly related to belong to a single
repository.
$ mkdir $GOPATH/src/github.com/username/hello
$ cd $GOPATH/src/github.com/username/hello
$ git init
$ ... more git setup
$ touch hello.go
$ gvim hello.go
$ git add hello.go
$ git commit -a -m 'Inital commit'
$ git push
Now people can do
$ go get github.com/username/hello
$ go install github.com/username/hello
to install your command hello.
It makes almost no sense to publish the content of $GOPATH/pkg at
the hosting service.
It makes some sense to publish the content of $GOPATH/bin at the hosting service. But I discourage this practice for obvious
reasons. Additionally, if you're publishing the sources - the
binaries are not necessary and everybody can build their (trusted)
own.
You seem to be perhaps still a bit confused by the term 'workspace'. A workspace is quite often existing only once at the developer's machine, yet it the typically contains several repositories. Some authored by the developer, others "go getted" from the Internet. To publish a whole wokspace in this case makes little sense.
However, there are people using a separate workspace per project or per repository or maybe even per package. I don't know what the benefits are. Or better said, I think that there are none compared to the single workspace, defined by, say export GOPATH=$HOME (as is my case for years w/o any trouble with it for years).
Check this link out for more details:
github go wiki on github code layout
below is a permanent section:
The app and both libraries live on Github, each in its own repository.
$GOPATH is the root of the project - each of your Github repos will be
checked out several folders below $GOPATH.
Your code layout would look like this:
$GOPATH/
src/
github.com/
jmcvetta/
useless/
.git/
useless.go
useless_test.go
README.md
uselessd/
.git/
uselessd.go
uselessd_test.go
README.md
Each folder under src/github.com/jmcvetta/ is the root of a separate git checkout.
If your not a fan of git cli then all you really need to do is upload to github repo via the web interface. Just make sure that you have your package name the same name as the repo (lowercase) and you should be good to go. I did just the same with github.com/Digitalblueeye/enroute for my REST API library.

Resources