Open brandondube opened 4 years ago
You mentioned go help packages
, but didn't get to the paragraph that covers this case:
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.
In that case, the “package” is still “a collection of source files in the same directory that are compiled together“ — it's just that the subset of the directory included in the package is determined by the command-line arguments rather than the usual build constraints.
I guess the interpretation boils down to where the active word in the sentence "A package is a collection of source files in the same directory that are compiled together." is. If being in the same directory is acting, then it states that all go files in a directory must be in the same package. It is a compiler (linter? At least tooling...) error when you have files that declare different packages in the same directory. The majority of writing on Go nods in that direction.
If being in the same directory is passive (a requirement, but not defining), then I would say the behavior of go build main.go
in this case is correct, otherwise I believe it is a bug.
There is a meta topic, too, of what determines what a package is at all -- can command line arguments restrict the scope of consideration for being a package? Why? Do the language rules allow sufficient ambiguity for there to be interpretation? Isn't the point of the language to be completely explicit, always, and remove ambiguity?
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I don't know
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
run
go run main.go
in a directory containing:What did you expect to see?
A successful compilation, on the basis that
go help run
begins withI wish to emphasize package and not file.
What did you see instead?
(build errors due to undefined symbols from lib.go)
The documentation for Go is very explicit in what a package is:
~ https://golang.org/doc/code.html#Organization
The output of
$ go help packages
perhaps disagrees:It may disagree on the basis of what an import path means. If import path ~= folder, then the file
main.go
is not a package, but the set of .go files in the directory is a package.If we are to say that
main.go
is given as an identifier for its package, thengo run main.go
should pull in all source files for the package. I think that ifmain.go
is given as a package in and of itself, this is different to an import path in the usual semantics of the language and the documentation (and wealth of tutorials, books, and so forth) about the language should also be updated.