go version)?1.9.2
Yes
go env)?Linux, amd64
$ docker run -it --rm --name test golang:1 bash
$ go get github.com/nats-io/go-nats-streaming
$ go install /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go
/go/bin/stan-pub binary installed, consistent with the requirements when go install <package> is used, just GOPATH is set and that should be enough, GOBIN should not be required.
Error message:
go install: no install location for .go files listed on command line (GOBIN not set)
Arguments of go install are documented to be packages, not files. Try
$ go get github.com/nats-io/go-nats-streaming
$ go install github.com/nats-io/go-nats-streaming/examples
$
or
$ go build /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go
Thanks @cznic for your comment. What I understand from documentation and implementation is that the name of the argument is packages, but .go files being passed is documented as special case (it's not documented that it works only if GOBIN is set, and I would like that to be fixed not to require GOBIN). I.e. see https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies
$ go install --help
usage: install [build flags] [packages]
Install compiles and installs the packages named by the import paths,
along with their dependencies.
For more about the build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.
See also: go build, go get, go clean.
Notice there go help packages reference.
See https://golang.org/cmd/go/#hdr-Description_of_package_lists
$ go help packages
Many commands apply to a set of packages:
go action [packages]
...
As a special case, if the package list is a list of .go files from a
single directory, the command is applied to a single synthesized
package made up of exactly those files, ignoring any build constraints
in those files and ignoring any other files in the directory.
...
If one go install github.com/nats-io/go-nats-streaming/examples, it results in unwanted behaviour, just first main/app in directory with 3 apps gets installed to /go/bin, stan-bench, with examples as binary name. Yes, example apps are preferred by convention to be in dedicated packages/dirs, but they aren't - I've logged improvement ticket for that (see https://github.com/nats-io/go-nats-streaming/issues/164). Even without them being in dedicated packages/dirs, by the docs go file name should be enough, it should be used as package name and resulting binary name. Since the special case docs does not mention that it additionally requires GOBIN to be set, implementation should be fixed not to require it, or docs could be updated to mention GOBIN is required in this case - I prefer former.
Second suggestion, to go build /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go builds stan-pub binary into current working directory, which makes it a partial workaround to go install inconsistency issue, but that does not fix the inconsistency.
This ticket is intended to fix the inconsistency in go install, inconsistency affecting many e.g. see https://stackoverflow.com/q/25216765/381140.
The use of GOPATH/bin only applies to packages stored in GOPATH.
In general GOPATH is a list of tree roots, and a package in one root gets installed to the corresponding bin directory.
go install /other/file.go is not considered to be in any GOPATH, so it can only use GOBIN.
In the long term, with modules, we expect that people will stop setting GOPATH and then GOBIN may be more important (or maybe it will increase pressure to default to GOPATH[0]/bin).
FWIW I would put the examples in subdirectories and then you can go install blah/blah/examples/...
Note that if we do make GOBIN effectively default to GOPATH[0]/bin, then we could make 'go env GOBIN' show that default.
See also #29005.
@newtorn stop posting links to your repository here and in other issues.
@jayconrod, @matloob: I think we should address this in Go 1.15. The inability to rely on $(go env GOBIN) when setting PATH makes new-user setup more complex than it needs to be.
sounds good to me
Change https://golang.org/cl/232017 mentions this issue: cmd/go: default GOBIN to GOPATH[0]/bin for packages outside GOPATH
Most helpful comment
@jayconrod, @matloob: I think we should address this in Go 1.15. The inability to rely on
$(go env GOBIN)when settingPATHmakes new-user setup more complex than it needs to be.