Installing gota package in go workspace - go

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.

Related

Understanding go mod and cause of package is not in GOROOT

I'm trying to play around with the lightning network. I have cloned the repo, and on disk placed it here (I'm using windows):
C:\Users\hallibut\Documents\GitHub\lnd
I'd like to run any of the tests in itest, lets say testMultiHopPayments. The cli commands I'm using after I cd into the above location is:
go test itest -run testMultiHopPayments
However, I keep getting the error:
package itest is not in GOROOT (C:\Program Files\Go\src\itest)
I've read through the various posts on this error, but I'm still not quite sure why it happens, and it's likely because I don't fully understand the go module (I'm new to go). This article, was probably the best in helping me understand the structure and env variables:
https://golangbyexample.com/workspace-hello-world-golang/
My understanding from the various readings is that whatever directory the go.mod file is in, indicates the module level directory. Prior to version 1.13 there was a required directory and structure, but now that should not be an issue if you're using at least version 1.13 and modules. I'm using 1.17.1. This is somewhat of an assumption or inference, but I believe everything lower in the directory structure is part of a package to be installed as part of the module (and is defined by the package keyword). However, I don't understand why a package with source code within a subdirectory would be missing/throw the aforementioned error. I've also tried running:
go mod install github.com/lightningnetwork/lnd/lntest/itest
That doesn't seem to do anything/has not effect on the error. What am I not understanding about packages? Looking at the go.mod file I observe that itest is not specifically defined anywhere… Not sure if that's required. Also, I assume I've got to run some build process prior? I attempted this with:
go install -v ./...
If you're using VS Code and Go Modules, you need to "Open folder" and point to the cloned repo, to get around that error

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 install Go package manually from source code

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.

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

Installing packages in a local directory

What is the best practice to install packages (those with go get...) in a local directory?
Example: I'd like to try out the Revel web framework, but I don't want to clutter my go installation at /usr/local/go.
Normally I'd say sudo go get github.com/robfig/revel as written on the home page, but that would install it beneath /usr/local/go/src/pkg/....
Is there an easy way to say (for example) go get --local ... and have the package in the current (sub) directory?
To expand on keks answer, you can update your .bashrc to look like this
export GOROOT=/usr/local/go
export GOPATH=~/workspace/me/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Now all packages installed with go get are separate from the go distribution.
You can export the env variable GOPATH. For me it is ~/local/lib/go. This folder has the subfolders bin, pkg and src, so it's just like /usr/local/go. The go-tool will then automatically download , build and install packages into this directory.
You might want to consider using Go Version Manager (gvm).
Apart from switching between Go versions easily, it also lets you switch between pkgsets ("workspaces").
First you create a set
gvm pkgset create myproject
and then you use it
gvm pkgset use myproject
Works like a charm.

Resources