The package declaration declares the name of the package.
The language Go doesn't know what a file or a directory is and the
import path itself doesn't effect the actual name of the package that
is being imported. So the only way the compiler knows what to call the
package is the package declaration.
> Also, since we don't have relative imports anymore would it make sense to
> allow having different packages in the same folder and allow importing them?
> At least this way you could break down your app in packages with access
> control to it's elements (public/private methods, structs, etc).
Packages are usually fairly large, much larger than a class in Java
etc. If you feel that making a directory is overkill for your package
then it's likely you've split your project up in to too many packages.
--
=====================
http://jessta.id.au
Yes.
> It seems like a unnecessary repetition.
You don't always come across files embedded in their directories.
It's nice to be able to look inside the file and see what package it
belongs to.
> Is it required by the compiler
It's part of the grammar.
> or is there any plan to remove it?
I've not noticed any, and it would, absent actual problems that removing
it would solve, seem to be ... unproductive. Also I think it would break
the Go1 stability contract.
> Also, since we don't have relative imports anymore would it make sense to
> allow having different packages in the same folder and allow importing them?
Why make things complicated when they're already simple?
> At least this way you could break down your app in packages with access
> control to it's elements (public/private methods, structs, etc).
You already can. Just put the different packages in different directories.
(Or did you mean to suggest having levels of visibility other than
unexported / exported? I rather like Go's binary visibility rule. Not
entirely convinced by the capitalisation, but the binary rule, that's
nice.)
Chris
--
Chris "allusive" Dollin
Yes, generally one project == one repo == one package is best
practice. Packages are not namespaces.
"Splitting into different packages" and "Splitting into different
repositories" are (more) closely aligned.
Bleagh.
Max of one repo per project I can sort of see, but I don't see why
there shouldn't be multiple projects based in one repo, to reduce
repo clutter if nothing else, and one package for an entire project?
That seems to me to be bonkers. I suspect we have strongly
different notions of how "big" a package or project is and how stronly
connected they are (or not) internally.
EG to pick a random hypothetical example, if one were hacking up
some Go tools to handle RDF, one would want a Turtle reader
(because RDF/XML is horrid) and one would want a NTRIPLES
writer (because that's the standard for test cases) and one would
want some base RDF stuff about IRIs and literals and whatnot and
at least one implementation of a graph thingy, and it seems to me
that they would certainly all belong in different packages and they
certainly wouldn't all deserve to be projects with their own repos.
For some value of "certainly".
> Packages are not namespaces.
Well, they /are/ namespaces. They're not /only/ namespaces.
import . notation is intended mostly for tests and other unusual situations and should be avoided unless necessary), so exported names in the package can use that fact to avoid stutter. For instance, the buffered reader type in the bufio package is calledReader, not BufReader, because users see it as bufio.Reader, which is a clear, concise name. Moreover, because imported entities are always addressed with their package name, bufio.Reader does not conflict with io.Reader."No, it states that you can leverage the fact that packages are
explicitly "namespaced" (more like "qualified") to make your public
identifiers succinct.
That said, after re-considering eg. crypto and vitess, and refreshing
"go build" behavior, I'll agree that for large projects, multiple
packages in one repository can make sense. But (to save a bit of face
:) --
> not allowing multiple packages in the same directory
> discourage people to structure the projects in subpkgs.
A subpkg certainly ought to be in its own [sub]directory.
Yes, I agree that a subpkg is better in a subdir. But then back to what started the conversation, why do we have the package keyword if it can be inflected from the subdir and why cant we have relative import, at least to subdirs (i.e. not alowing ..)
Yes, I agree that a subpkg is better in a subdir. But then back to what started the conversation, why do we have the package keyword if it can be inflected from the subdir
go get github.com/kuroneko/go-sqlite3
import (
"github.com/kuroneko/go-sqlite3"
)
func main() {
sqlite3.DoSomething()
}
In this case, the name of the package cannot be in any way, the name
of the subdir, since go-sqlite3 isn't a valid go identifier. That's on
reason for having the package statement.
Consider this:
The import path is the location where you get some package,
The package stmt is the name that the author desired for his package.
If you have two different versions of the same package you can change
only the import path and not the package name, making them coupled
would break this. Also, you can always create a alias for packages.
--
André Moraes
http://andredevchannel.blogspot.com/
"Separation to reduce coupling" and "using different packages" are
orthogonal actions. You can do the former without doing the latter.
"Splitting into different packages" and "Splitting into different
repositories" are (more) closely aligned.