go install: no install location for directory outside GOPATH - macos

I installed the Go, then added path requirements to my .bash_profile:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
I then setup the correct folders:
I also created a projected called tire.
The contents for main.go are simply:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
I always get the following error when I try to run go install:
go install: no install location for directory /Users/Daryl/go/src/tire outside GOPATH
Here's what I get when I run go env:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/daryl/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
TERM="dumb"
CC="clang"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common"
CXX="clang++"
CGO_ENABLED="1"
Any help would be appreciated.

When you run go install Go looks for $GOBIN env variable path. Either you need to set your $GOBIN to $GOPATH/bin
$ export GOBIN=$GOPATH/bin
and/or add $GOBIN to your OS search path
$ export PATH=$PATH:$GOBIN
To use the command without getting the error.

The problem was as James Henstridge commented, for some reason there was an issue with my user directory name case. Even though the directory is lowercase, I had to make it capitalized.
This worked:
GOPATH=/Users/Daryl/go
This didn't:
GOPATH=$HOME/go
However, since moving to a rMBP from my iMac, I had no problems whatsoever setting up Go, so, to this day, I'm not sure what was going on, but in that instance the capitalization fixed it.

I had the same problem in Windows 10,
So I set a system variable named GOBIN
with absolute value F:\go\bin.
Then ran go install main.go and everything worked perfectly fine!

Related

How to confirm that $GOPATH and $GOROOT are properly set?

This is not a repeat of this question:
what should be the values of GOPATH and GOROOT?
I don't want to know what the values should be. I want to know what I should see when I type ls $GOROOT or ls $GOPATH into console. I'm pretty sure I set things up wrong following a tutorial almost a year ago, and I want to be able to confirm that these two are pointing to where they should be by simply checking that what they point to looks right.
Here's where I am right now. It looks like $GOROOT is pointing nowhere. I'm pretty sure it should be pointing at usr/local/go, but it would be a lot easier to confirm if I knew what the expected result of ls $GOROOT is supposed to be.
As for $GOPATH I'm not totally sure if my "workspace" is where all my go code is, or maybe just the github stuff, or maybe the particular folder I'm working within. I know it's supposed to point to my "work space," but I don't know what that work space I'm looking for looks like.
Sephs-MBP:ThumbzArt seph$ $GOROOT
Sephs-MBP:ThumbzArt seph$ $GOPATH
-bash: /Users/seph/code/golang: is a directory
Sephs-MBP:ThumbzArt seph$ ls $GOROOT
Bman.jpg README.md ThumbzArt.sublime-workspacescripts thumbzart.go
LICENSE.md ThumbzArt.sublime-project public templates ticktock.go
Sephs-MBP:ThumbzArt seph$ $GOPATH
-bash: /Users/seph/code/golang: is a directory
Sephs-MBP:ThumbzArt seph$ ls $GOPATH
- bin p pkg src
Sephs-MBP:ThumbzArt seph$ ls /usr/local/go
AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test
Sephs-MBP:ThumbzArt seph$
I know this question seems ridiculous, but it's hard to confirm things for which you have no expected results.
Thank you
Sephs-MBP:streak seph$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/seph/code/golang"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/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"
Sephs-MBP:streak seph$ go version
go version go1.5.2 darwin/amd64
Sephs-MBP:streak seph$ which go
/usr/local/go/bin/go
EDIT: Another very useful feature I just stumbled across is this: go help gopath. This should have probably been somebodies answer.
The folder $GOPATH points to should like this:
Sephs-MBP:streak seph$ ls $GOPATH
- bin p pkg src
$GOROOT, on the other hand, will yield unexpected results if you use ls $GOROOT as compared to ls $GOPATH. This is because $GOROOT is not set within this context, I think.
Sephs-MBP:helloworld seph$ ls $GOROOT
helloworld.go
If you use go env you'll see the true nature of $GOROOT
Sephs-MBP:streak seph$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/seph/code/golang"
GORACE=""
GOROOT="/usr/local/go" //this is where it actually points
GOTOOLDIR="/usr/local/go/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"
And if you get the result of $GOROOT from go env and do ls you should see something like this:
Sephs-MBP:streak seph$ ls /usr/local/go
AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test
If all of these things check out, then your $GOPATH and $GOROOT are properly set.

Go package imports self, and can't find itself in it the GOPATH

Every once in awhile, I'll use a go package that has a go get like : go get github.com/<user>/aRepo and in its own import statement have import "github.com/<user>/aRepo". So it imports itself using a the "go gettable" path. Sometimes this works fine; however, sometimes I get
package github.com/<user>/aRepo
imports github.com/<user>/aRepo
imports github.com/<user>/aRepo: cannot find package "github.com/<user>/aRepo" in any of:
/usr/local/go/src/github.com/<user>/aRepo (from $GOROOT)
/home/me/go/src/github.com/<user>/aRepo (from $GOPATH)
I set my GOPATH with:
export GOPATH=$GOPATH:/home/me/go:`godep path`
It's very unclear as to why this would happen, any ideas as to why?
EDIT
my go env:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=":/home/vagrant/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
So it turns out it was a permissions issue since I was using a vagrant box with a synced folder, vagrant set the entire path that synced folder created as 744 root:root. Therefore, I could not write to that folder as a non-root user, so that error message is actually coming from godep trying to install itself and not finding its src.

Go lang installation issues with ./make.bash ubuntu->windows

I have tried to search and found a lot of topics which are related to my problem, but none of them I could follow to success.
I can go run and go get with no issues, but I need to compile into windows and there I have problems please see bellow
mikhail#mikhail-desktop:/usr/lib/go/src$ sudo ./make.bash
# Building C bootstrap tool.
cmd/dist
go tool dist: $GOROOT is not set correctly or not exported
GOROOT=/usr/share/go
/usr/share/go/include/u.h does not exist
mikhail#mikhail-desktop:/usr/lib/go/src$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/mikhail/Documents/FL/0go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
mikhail#mikhail-desktop:/usr/lib/go/src$
By some reasons it thinks the GOROOT is /usr/share/go while it actually /usr/lib/go
sudo can clean environment variables, to check if that is the case here, run sudo $(which go) env and check if the output is what you expect. If not, you can keep the enviroment of your user by using the -E flag: sudo -E ./make.bash.
I think it somehow connected to Debian/Ubuntu thing. I seen such issues but related to 0.9. version.
I have installed go from source into ~/go and then everything went quite OK, now I can compile to windows from ubuntu with help Introduction to cross compilation with go and LiteIDE.

Go 'Can't Find Import'

I'm attempting to incorporate Heroku's log-shuttle library (https://github.com/heroku/log-shuttle) into a Go project I'm working on. The tool is primarily designed to be run as an independent binary, but I'm hoping to integrate it in a different way in my tool.
So I get the library:
$ go get github.com/heroku/log-shuttle
$ ls $GOPATH/src/github.com/heroku/log-shuttle/
batcher.go Godeps ...
...
which returns successfully. Then I try to import the library:
package myPackage
import (
"github.com/heroku/log-shuttle"
"fmt"
"log"
)
...
Great. But now I go to go build and...
$ go build
# github.com/<my project>
logWriter/log_shuttle_writer.go:5: can't find import: "github.com/heroku/log-shuttle"
I have my GOPATH set correctly (I believe):
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jeff/go/"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
And the package is installed and built, but it doesn't want to import. As you can see, there are non non-ASCII chars in the package path (though the hyphen is non-alpha-numeric), and I can't find any more info about what could be causing this problem.
Not sure how it would matter, but I am using godep to try to manage my dependencies.
Thanks in advance.
github.com/heroku/log-shuttle is a main package, not an importable library, meaning it's meant to be compiled and run as a binary.

Why is go install writing binary in the src directory?

I have a small go program in the directory:
~/gocode/src/github.com/elviejo79/goexample/
When I execute
go install
The program does compile but it leaves the executable in the same directory as the code.
but according to How to Write Go Code the binary should in fact be saved at
~/gocode/bin
this my $GOPATH
$ echo $GOPATH
/home/agarcia/gocode
Cannot reproduce this. Please Enter
$ go env
And verify where your GOBIN points to. For example, at the machine I'm right now, it says
09:01 myname#tux64:~$ go env
GOARCH="amd64"
GOBIN="/home/myname/bin"
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/myname"
GOROOT="/home/myname/go"
GOTOOLDIR="/home/myname/go/pkg/tool/linux_amd64"
CGO_ENABLED="1"
09:01 myname#tux64:~$
The go tool should send the binary to $GOBIN. Here it works like that.

Resources