'"go get" command not generating the bin folder when it is run in a shell script
I have installed go package. When I go to the instance(VM) and run the command
go get github.com/linkedin/Burrow
from a terminal/cmd it is downloading both "src" & "bin" folder under user home directory. But when I run the same command by setting GOPATH in a shell script, it is only downloading the "src" folder but not generating "bin" folder.
SOURCE_DIR="/opt/burrow"
export GOPATH=$SOURCE_DIR/go
go get $BURROW_REPO
Am I missing anything?
Solution 1:[1]
Use go install
command (Compile and install packages and dependencies).
That command download src
to $GOPATH
and build it to $GOBIN
.
Solution 2:[2]
There's a lot going on here.
The first point of your confusion is that go get
does not download neither "src" nor "bin": Go packages are always contain only source code, and they typically does not contain the "src" directory in their file and directory hierarchies.
Instead, these directories are artefacts of the the Go toolchain.
The second point of confusion is that since Go 1.8, the Go toolchain uses a fallback value for the GOPATH
environment variable if that is not set, and on Unix-like systems it defaults to the directory named "go" under the "home" directory of the user executing the go
command.
If this directory is missing, the toolchain will create it.
Hence my stab at your problem is that you have some sort of permissions problem: when GOPATH
is unset, "$HOME/go" is used — with whatever value $HOME
expands to for the current user; when you set GOPATH
by hand, something prevents creation of the "bin" directory under $GOPATH
.
There's another possibility: you also set the GOBIN
environment variable, which, when set, overrides the usual location used to install binaries built by go install
(and go get
).
You might study the output of go help environment
to read more on the subject.
In either case, the most sensible path forward is to run go install
with the -x
command-line option and see where the command tries to put the generated executable image, and why this fails (if at all).
You might study the output of go help install
and go help build
to read more on the subject.
You might also consider forcing usage of Go modules for your particular case:
running GO111MODULES=on go get github.com/linkedin/Burrow
would work like five times faster for your use case.
Be sure to study the output of go help modules
and go help mod-get
first.
Solution 3:[3]
bin
folder or src
folders are not automatically created for you by the go get
command. Here are the standard steps creating a new project in go assuming this the first time you are creating a project in go:
- Under your workspace directory, create a project directory, say "project1" bin, src directories.
Set GOPATH, GOBIN:
export GOPATH=~/workspace/project1/
export GOBIN=~/workspace/project1/bin
Now if you need just the source code, do a
go get github.com/linkedin/Burrow
or if you need a binary do ago install github.com/linkedin/Burrow
The binary will be stored under ~/workspace/project1/bin and source code under ~/workspace/project1/
Same steps would apply either if your creating your project through a make file or terminal
Solution 4:[4]
GOBIN may not be set at the moment.
You can check by
go env GOBIN
. if its empty so you must set withexport GOBIN=$(go env GOPATH)/bin
Also, for calling binary files from your terminal, you need to use
go install
command. This will create related bin file under GOBIN path.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | kostix |
Solution 3 | Ashwin Shirva |
Solution 4 | Alteran |