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

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.

Related

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.

go get installation from a local fork

I am trying to install a go package from a local directory (basically I checked out and existing package and applied a pending pull request).
$ # Both commands give a similar output
$ go get -u file:///Users/me/workspaces/go/somepackage
$ go get -u /Users/me/workspaces/go/somepackage
unrecognized import path "[...]" (import path does not begin with hostname)
Since go get is downloading then installing, I tried:
$ go install /Users/me/workspaces/go/somepackage
[] cannot import absolute path
Any experienced go user could give a hand?
If you just want to use a different branch (your PR branch), you can continue using the original import path.
Go get the original package first.
Go into the downloaded package in your local file system cd $GOPATH/pkg/<package directory>
From your local package cache, switch to the branch you want to pull from. git checkout <PR branch>
Now go get -u <package>
If the package is available locally, go get update will just pull the latest code from the branch your local package is checked out to.
As far as I know you can't do this with go get, bur you can copy the directory in to GOPATH manually. If you forked example.com/somepackage then copy /Users/me/workspaces/go/somepackage to ~/go/src/example.com/somepackage or ./vendor/example.com/somepackage.
That being said, the best solution is usually to host the forked code somewhere, and then use that import path. Decency tools such as dep and modules support fetching a different origin for packages.
I could be wrong and maybe there is a workaround exists
Go documentantion on cmd/go/#hdr-Remote_import_paths says:
The supported version control systems are:
Bazaar .bzr
Fossil .fossil
Git .git
Mercurial .hg
Subversion .svn
And a later:
For example,
import "example.org/pkg/foo"
will result in the following requests:
https://example.org/pkg/foo?go-get=1 (preferred)
http://example.org/pkg/foo?go-get=1 (fallback, only with -insecure)
So I suppose that go get never looks in filesystem repos.
Need to investigate source code of go get to be sure.
I will be glad if someone will prove that I am incorrect in this question.
UPDATE
Maybe I'm mistaken but I suppose that all abilities of go get to work with VCSs are here: https://github.com/golang/go/blob/master/src/cmd/go/internal/get/vcs.go
And I don't see here a possibility to work with git local repos.

Issues with 'go build' on forked repository

I build a binary file for a GitHub repo (go code), which works fine. no issues.
I forked that repo, and modified a single line in the HTML file that has nothing to do with GO code, built the binary file for the new forked repo but the binary it generates refers to the original repo code, can't understand why.
I even cleaned all the code using go clean -i all command and manually removed all the installed code, binary files from $home/go/bin and the repo directory, but it still refers to the original repo code instead of new forked code.
Based on the solution suggested by Tobias, I performed the following steps:
After that, I executed go build in that repo directory, but the new binary file still refers to the old code. I even removed the old binary file and generated a new one.
That's a common problem in go. The references system in "location based" so it searches for these files in the "correct" path. Idk if go modules fix this issue, but atleast when not using go modules you'll have to work around it.
You can solve it by
Solution 1
Download the original repository you forked by:
go get http://github.com/awesome-org/tool
Add your fork as remote
git remote add awesome-you-fork http://github.com/awesome-you/tool
You'll have to make changes in the folder of the original downloaded repo and Push and Pull to/from your fork.
git pull --rebase awesome-you-fork
git push awesome-you-fork
Solution 2
Work around go get:
You create the path the original repo would have, but clone your own fork into it. That way you can push & pull to your fork. That may be the better solution
cd $GOPATH
mkdir -p {src,bin,pkg}
mkdir -p src/github.com/awesome-org/
cd src/github.com/awesome-org/
git clone git#github.com:awesome-you/tool.git # OR: git clone https://github.com/awesome-you/tool.git
cd tool/
go get ./...
These Solutions were found here: http://code.openark.org/blog/development/forking-golang-repositories-on-github-and-managing-the-import-path
The problem with a forked copy of a go packages is when the package is really multiple go packages in one repo, the import statements refer to original base repo, ie: github.com/orig/repo.
This is not an issue for repos with only one go package as it never refers to itself.
But if it has multiple, ie: package github.com/orig/repo/A imports github.com/orig/repo/B
And then you fork it as: github.com/fork/repo
Then when the go compiler sees import "github.com/orig/repo/B" in the source, it goes to download the original version and not your fork.
Fortunately, go modules solves this.
Basically, create a go.mod at the top of your forked repo and add:
module github.com/orig/repo
then, the go compiler will assume that you are "orig/repo" regardless of where you actually are checked out from.
so, when orig/repo/A imports orig/repo/B, it will look locally.
If there are other imports you need to override that are outside the main forked repo, you can also force dependencies to come from another place using replace
SOLVED
At first I thought it was problem with a FORK (which is a common problem experienced with fork of Go language repo's), but it turns out, it was Repo specific problem.
One of the dependent libraries had to be reinstalled for the forked repo to work, which was not mentioned in the Original repo docs.
Finally, this link helped: https://github.com/inconshreveable/ngrok/issues/181#issuecomment-65646229, the problem was not generic but repo specific
I followed the below steps from above link to resolve the dependency on go-bindata
go get github.com/jteeuwen/go-bindata
cd $GOPATH/src/github.com/jteeuwen/go-bindata/go-bindata
go build

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.

godep: exec: "go": executable file not found in $PATH

I trying to deploy my server on heroku and I stuck on step where I should use godep, I spent little time with GO, last two days I had looking how to solve this issue, actually it's a popular issue, but I can't figure out, maybe I did something fundamentally wrong.
I have OS X 10.11.2
My GOPATH - Users/denis/Programming/Golang
My PATH - $GOPATH/bin
I trying to use godep to my project which is placed in $GOPATH/src/backend
in my PATH I have an executable godep file(not .go).
Whole structure of my workplace.
/Golang/
.bin/
godep //executable file
.pkg/
.darwin-amd64/
.//other folders/
.src/
.github.com/
.backend/
.//other folders//
First, what is the output of this:
$ echo $PATH
It should, at a minimal to run Go projects, have TWO directories in it for:
/path/to/go/installation/bin (e.g. /usr/local/go/bin)
/path/to/your/GOPATH/bin (e.g. /Users/denis/Programming/Golang/bin)
Your go's installation should be in your PATH already if you followed the Go installation setup.
https://golang.org/doc/install
Since you posted /Users/denis/Programming/Golang, with the capital U, I am going to assume that you are on OS X going forward...
In OS X, and if you used the default install, you can test for Go in your PATH with a simple command:
$ echo $PATH | grep --color=auto "/usr/local/go/bin"
It should print our your entire $PATH and highlight that you have things setup properly. This is because the OS X GoLang installer in the URL above should have modified your PATH to include:
/usr/local/go/bin
or more specifically, it may be export PATH="$PATH:/usr/local/go/bin"
If you have some custom .bashrc and/or .profile files, then most likely your PATH isn't being setup correctly. You can test this by doing this:
$ export PATH="$PATH:/usr/local/go/bin"
$ export PATH="$PATH:/Users/denis/Programming/Golang/bin"
$ godep go build
If it works now, then your PATH isn't setup properly in your .bashrc/.profile files. That's a different kind of question and can be setup a 1000 different ways and you may need to figure it out (or another SO question).
If that resolves your issue, then you need to follow the godep directions to run godep from your project's root:
$ cd $GOPATH/src/backend/
$ godep save
$ git add Godeps/
godep creates a Godeps directory in the root of the Go project. If you have multiple Go projects/multiple executables, then you need to run godep save on each root of the runtime.
IOW, for each executable you run go build for, you need to run godep save in each directory.
Also, once you move to godep, you want to use it for your build and testing as well:
$ godep go build
$ godep go test
...etc
If that's not the problem (you are running it as stated above), then please update your question with more specifics such as what godep command you are running, where, and what that directorys structure looks like for the root of that project (including the filename with package main and your func main() function).

Resources