3 ms·
Re package paths, I guess the tradeoff here is for go get/install etc to be able to include or fetch dependencies, it needs to know where to put them and instal
by grey-area 14y ago
Re package paths, I guess the tradeoff here is for go get/install etc to be able to include or fetch dependencies, it needs to know where to put them and install them, and this was the simplest system they could think of which worked. Nowadays I think it's ~/.go/src/github.com/user/pkg rather than just ~/.go/src/pkg It would be nice to be able to specify goinstall and golib paths or something though, and I guess your symlinked idea is a good workaround. I have just gone with the flow on this one, and don't mind as don't have gopath at the root of ~/.
Re folders versus sub-packages, yes I take your point - again there are some conventions here they've tried to impose to make it simpler for the package tools, but those could get in the way. I also hate projects that have all the files piled into one dir, so I prefer to break into sub-modules where possible and make the organisation clear. The other advantage of this is that people could use just your sub-module if they want.
I've developed a few packages which I wanted subfolders for, but those subfolders naturally became sub-packages without much wrangling - seeing too many files on one level forced me to organise them and create more packages. One case for example mirrors your usage - a top level pkg, with interfaces and concrete types for specific databases, which lived in their own folder 'adapters' this became a sub-package, which contained the interface and concrete implementations, and it being a package forced me to remove any knowledge of the enclosing package and any circular refs - the top level pkg knows about the adapters, but adapters don't know about the top level. I felt like this was making the code better, not worse, by removing internal dependencies, but YMMV, and I can see how it would be annoying. I'm willing to forgive conventions like this if the tradeoff feels worth it (in this case I really like the go gettable packages, and to have that am willing to live with restrictions like some code must be in root, folder=subpackage, etc.).
- lobster_johnson 14y agoWith the subpackage, did you actually put the stuff in subfolders of the original package, or did they have two different top-level folder names? In other words, was it import ( "github.com/foo/x-core" "github.com/foo/x-adapters" ) or import ( "github.com/foo/x" "github.com/foo/x/adapters" ) If the second, then do you need to do something special with regards to compilation or anything else? One thing that bugs me with the second approach is that the package names then have to be different. If the original core package is called "x", and you have an "adapters" package, would the package name be something like "x_adapters"? Just "adapters" seems a bit presumptuous (it's very generic).
- grey-area 14y agoBecause packages are namespaced by user, and it's easy to rename imports, I don't worry about choosing obvious names - that's only a problem if it is popular and used by more people than me anyway :) So I'm using obvious names, with the pkg (say core) in one folder, and subpkg in the subfolder named adapters. "github.com/user/core" this has a few .go files in it to split things up into different concerns, and a subpackage with adapters (again with a few files), imported with: "github.com/user/core/adapters" and using adapters.XXX in the core code. You can also do this: import ( mypkg "github.com/user/core/adapters" ) and use mypkg.XXX to refer to the subpackage, so you're not tied to the folder names for package import names, that's just the convention, and if someone needs to rewrite the name on import, they can do so.
- lobster_johnson 14y agoTrue, although it gets annoying having to "rename" a package like this. If it's a package you use everywhere in your project, it gets old fast; canonical, author-defined names are always the best option.
- grey-area 14y agoYes, it could be a pain unless it was just imported in one or two files. In practice though I've not found collisions in package names to be an issue, simply because of the layering of packages - the top level package is really the only one which knows about these adapters and imports them - it then presents an interface to the world which is under its namespace, so the only worry for uniqueness is the top level pkg name really.