转到install:GOPATH以外的目录

I've just installed Go and I can't get my go install working. I followed a tutorial on YouTube to write a HelloWorld.go app but I keep getting the error:

go install: no install location for directory /Users/####/Documents/Dev/go/src/github.com/####/hello outside GOPATH

Here is my go env:

GOARCH="amd64"
GOBIN="/usr/local/go/bin"
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/####/Documents/Dev/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

In the tutorial that I was following the persons GOPATH was also set to a folder and then I was meant to create the folders /src/github.com/.....etc and then run go install in the directory with my code. Here's the code, although I imagine that not much could be wrong with the code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!");
}

When I run the pwd command inside the directory with my code, this is the output: /Users/####/Documents/Dev/go/src/github.com/####/hello

When I run the echo $GOPATH command inside the directory with my code, this is the output: /Users/####/Documents/Dev/go

I am running Mac OSX Yosemite.

I can think of two possibilities:

  1. GOBIN environment variable

I'd suggest you read documentation about the go command, specifically the section on the GOPATH envvar. Quoting:

The bin/ directory holds compiled commands. Each command is named for its source directory, but only the final element, not the entire path. That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped so that you can add DIR/bin to your PATH to get at the installed commands. If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin.

Go is likely trying to install your program here, and running into permissions errors. Might just be a poor error message. In any case, it's unclear to me why you have $GOBIN set. To add the go tool to your PATH, add the directory directly: export PATH=/usr/local/go/bin:$PATH

  1. Case-insensitive filesystem

If that doesn't work, check the case of your home directory, as was the problem in https://stackoverflow.com/a/27430341.