Reputation: 2727
I am using go version go1.5.1 linux/amd64
on debian 8.2 3.16.0-4-amd64
. I installed golang
using https://golang.org/doc/install.
This is what i put in my ~/.profile
file
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:/home/shivams/go/bin
Running go env
on my machine is giving this output
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/shivams/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
Directory structure inside /home/shivams/go
is {pkg,src,bin}
. Inside src directory it is like test/hello.go
.
If i am in src
dir and run go run test/hello.go
it is running perfectly. Also if i run go build test/hello.go
it will create one executable hello
file in same directory.
But if i try to run go install test/hello.go
then i get this error
go install: no install location for .go files listed on command line (GOBIN not set)
. If i set GOBIN
explicitly then i am not able to see this error.
From what i read my understanding is that if GOPATH is set there is no need to set GOBIN variable.
Am i missing anything here? This is the first time i am trying go and not able to get this working.
Upvotes: 7
Views: 34744
Reputation: 12685
According to Games Brainiac
go install work on packages is correct. But let me add some details into it regarding creating packages using go install in main directory.
.
├── bin
│ └── app
├── pkg
│ └── linux_amd64
| └── user
| └── handlers.a
└── src
├── bitbucket.org
├── github.com
└── user
└── app
├── main.go
└── handlers
└──handlers.go
Have a look at above directory structure, I have created a package in a folder handlers having source file handlers.go
.And I wants to build that package, I will run go build
inside handlers folder which just show our file has no compilation errors and returns nothing in that case.
To build a package with main file we need to run go install
inside app folder. This will create handlers.a package object inside $GOPATH/pkg
and executable file inside $GOPATH/bin
with name of folder (app in this case)
in which we have main.go file using package main.
Upvotes: 2
Reputation: 82590
As @JimB states, install
is a command designed for packages. Just to give you a clear example, here is what I get when I run the go env
command.
GOARCH="amd64"
GOBIN="/Users/quazinafiulislam/Code/go/ogolang/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/quazinafiulislam/Code/go/ogolang"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.5.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT=""
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
You will see that the GOPATH
is set to my project root, ~/Code/go/ogolang
. Now, lets see whats inside my project root.
.
├── bin
├── pkg
│ └── darwin_amd64
└── src
├── bitbucket.org
├── github.com
├── golang.org
├── words
└── wordtest
As you can see I have a couple of packages. One of them is wordtest
. I can use a go install
on the words
or wordtest
packages. So, lets run go install words
and see what happens to the directory.
.
├── bin
│ └── words
├── pkg
│ └── darwin_amd64
└── src
├── bitbucket.org
├── github.com
├── golang.org
├── words
└── wordtest
Now, we can see that we have a words
binary created for us(inside the bin
directory), and we can run it by invoking ./bin/words
.
Upvotes: 13