-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg directory should not be recommended for use #10
Comments
@theckman Interesting! I'll certainly consider this adopting this. What is the general reason that using the |
Can you be a bit more specific about the 'we' part. Who exactly are you talking about. Is there an official community consensus around this (controversial) pattern? Is there a consensus from the official go dev team? At least, the last interaction around it on the gophers slack channel is limited mostly to you saying don't use the 'pkg' pattern :-) There might be other threads and discussions I didn't see and that's why I'm asking. This repo attempts to capture the existing or emerging patterns in the Go ecosystem / project community. The 'pkg' pattern is used in a number of high profile and popular projects. Some of those projects might not be great examples others should follow, but it would be a bit suspicious to say that all projects leveraging the 'pkg' pattern are bad examples :-) Of course, it's not something that the majority of projects out there do and it appears there are some strong feelings against it, but i'm not sure if there's a unanimous agreement that it's a bad pattern. I definitely agree that it's a confusing pattern for new go devs. I'll be happy to add more context and the necessary references to the emerging community recommendations. |
@rburmorrison I'll start by saying I'm a proponent of For The second reason kind of has to do with long paths, but it's also that |
@kcq I'm speaking in terms of what has become a general consensus in the Go Slack and Forums areas. The Go authors themselves have been absent in providing guidance on this topic, rightfully so. There are discussions beyond ones involving me (based on the interactions I've seen), and my opinion was shifted from liking
I think one issue I have with explanations like this, is that the |
I don't know if a Tweet can/should be used as source but Brad Fitzpatrick states pkg is legacy: https://twitter.com/bradfitz/status/1039512487538970624 |
@nineinchnick yes, it's a 'legacy' pattern for Go itself. Brad's tweet provides a nice background about the origins of the pattern :) |
@theckman While I'm not a Go developer, I do know a little bit about development in general terms. You said:
The problem is you made a direct conclusion that if something is not in I personally like the idea of |
@hron84 In Go you can put it at the top level of a package so all things inside of that repository can use it. If you are looking for code reuse across repos, that means you expect it to not be internal and consumed externally. If you consider that, I don't think my statement is a stretch. |
pkg is not a canonical practice to organize "importable" packages. One side effect of representing it here is that I hear a lot from others that I should reorg my packages as suggested as here.
I don't think there is a widely accepted rule for this. This is the first time I am hearing this intention and I personally know very few people who thinks "pkg" makes a package intended for public use. Any importable package is for public use. Anything else goes into internal. Note that internal package is not just a convention, it is supported by the Go tool. |
The This value proposition seems clear and unambiguous to me. I don't understand why some Go programmers are so vocal about squashing it. I don't think it is mandatory, and I wouldn't suggest advertising it that way, but it's clearly useful and I would push back against (or at least ignore) anyone who suggested it not be used. |
@peterbourgon It's not clear and unambiguous to me, because it's honestly not clear what value it adds beyond cognitive overhead and unnecessary abstractions. To me it's just noise. I don't really care what directories my Go source code is in. What makes you care? My editor has jump to definition, and I have other tooling I can use to discover files based on their declared types. I also have an easy way to open any file in my current project using a simple keyboard shortcut. It's been superseded by better tooling, functionality, editor integrations, and assumptions by the Go toolkit, and we should retire it as a relic of times past I also have issue with this quote in the README:
No, anything that's NOT in |
If I'm looking for Go code, I know where to look. Equally importantly, I know where not to look: is For example:
You are affirming the consequent. |
Hmm, that makes me less convinced. If I'm looking for Go code I generally have an entry point. A function, a file, some string to search for, etc. I use my editor's tools to do the work beyond that. It seems like a pretty inefficient workflow to just go spelunking in repos looking for Go files without any other way to drill-down to what you want to find. If someone wants to keep all the |
Well, I guess it's moot, really: despite the rather lofty implications of the name of this GitHub organization and repo, there is no standards body which canonizes an e.g. repo layout, only the collective opinions of Go programmers. I'm happy to let time be the judge of this practice. I'll bow out now. |
(I don't have any strong opinions whether this can be a canonical practice and please say so if this is a bikeshedding discussion.) I think the question is whether location and organization should tell anything about a Go package or not. Organizing packages under pkg/ makes their import path include "pkg" and advertise that they are not main packages. It might be an arbitrary distinction which has a lot of impact on the package identity. Tools might benefit from the distinction but there are no tools that blesses this convention (yet). |
There's no mention of What I can understand from the doc is: create packages only if you really need to. That basically means that you should avoid exporting internal structs and methods, possibly keeping them together with the main package. |
@giefferre the |
@peterbourgon It's ok if not everybody finds the |
A very new Go developer, but as an experienced developer. Here are my thoughts. I generally like the idea of pkg/ but I'd say doing things the community-accepted way is more important. I wish there was a definitive answer. For
|
- pkg is out, see golang-standards/project-layout#10 for inspiration as to why - Controllers are now in the 'controller' directory - ux is 'view', to give a whiff of MVC to the project - various other rearrangements - fix a long-running lint fail
* xsensemulator: separate package for emulator * Remove pkg prefix directory golang-standards/project-layout#10 (comment)
* xsensemulator: separate package for emulator * Remove pkg prefix directory golang-standards/project-layout#10 (comment)
So first I like to gently push back on a few of these points:
Maybe it's just the projects I've worked on, but I do think something being missed is that packages located in "pkg" also imply they have no dependencies on any packages in the rest of the project not located in pkg and it lets other developers working on a repo know not to add them. Primary, top-level library functionality the project wants to expose is still done so outside pkg. I typically use this to incrementally pull out common patterns into their own frameworks before moving them into entirely separate repo. So, you could call discoverability, glanceability, and organization "cosmetic" but so are a lot of language structures. |
I have described in some detail the concrete and technical benefits |
I believe this repository should be renamed to |
I prefer to The latter is less concise. The word Having the word |
if i write some code which will be used in many micro-services(for example generated protobuf + some methods) i can use pkg folder and init git submodule and develop that part near service and uc? |
I've always found "pkg" useless. 1. it's a 3 letter shortening, which isn't friendly to non-English speakers who may think it's an acronym for something (P.K.G.?) 2. it's a useless name that tells you nothing about the content. Why "package"? Every directory that contains .go files is a package. It's basically the same as creating a folder called "files". I recently changed some codebases to use pkg (to be fair we were using "src" which is equally as bad) but I feel like just moving our three subdirectories to the top level would make the most sense. |
This whole discussion is pointless as everyone should use the layout they like, absolutely no one should dictate how your structure looks like as long as this is not enforced by the programming language. Most of the contributors in this issue try to convince everyone to follow their preferred way which is totally selfish and the root of a lot of problems. After all everyone can use I'm a
This means your project should never contain any directory because your documentations, deployment manifests, scripts and whatever is part of your repository are also only “files“. Anyone must understand that a repository of a Go package is, just like any other project written in a different programming language, more that just your Go “files“, documentations and everything else is part of it and should be separated accordingly. Many large Go project suffer from contributors because some maintainers try to follow this non-sense “is does not feel like Go“ way of structuring which discourages potential contributors because they'll need to spend days to get an overview of the mess of data that lives in the project root. There are directories named The This comment is not meant to be rude and also not to criticize others but more an eye opener to finally see the whole and not only 3 characters in an important path. A project repository is more that just Go packages and no matter how the maintainers decide to structure it in the end, it is their decision and no one has the right to judge it. |
I think you missed my point haha, maybe it was poorly written. My point was that since every directory of Go files is considered a "package" by the compiler, it doesn't tell the reader much by grouping together some packages and using a name that simply indicates that you've grouped together some packages. You can tell that by observing that there are Go files in there! The analogy regarding "files" is more about superfluous nomenclature, nothing more. Essentially, tell your future readers what the intention is behind your code, clear and simple. But also, I do agree that it doesn't really matter in the grand scheme of things. If you're building a product at a company, optimise for things that matter and longtermism. You don't onboard a new developer every day of the year all year round but your team works with your code for many years, so optimise for that. Which means don't waste time figuring out the "best" folder structure - your engineers will learn how to navigate regardless. |
My opinion is that It would be great if in those cases we could think that the stuff under such project's If the project is more like a library, something that doesn't do anything on its own, like a logger for example, the whole reason for its existence is to be imported from outside. In that case, the I want to import When I need to import the XML formatter for superlogger, I expect to import it from Maybe the purpose of this repository is to be an example for an application project layout, not a library layout. |
The general consensus of the Go community has been converging on the idea that the
pkg/
directory is a useless abstraction / indirection. Instead, people are encouraged to either put the packages in the top-level of the repository, or to create a repository structure that structures things based on their primary functionality of domain in the business logic.So while in your example you may encourage people to have the following layout:
We instead would prefer:
This may seem innocuous, but because this project has asserted itself as some sort of Go standards body what is being recommended here is being taken seriously. In the Slack workspace, where there are over 30k registered users, we quite often need to correct people who are using
pkg/
and have them ignore the recommendations here. Some of the things recommended here are good, and I'd like to be able to use them as a resource. That said, considering the currentpkg/
recommendation we're hard-pressed to use this as a resource because we have to tell people to ignore certain parts.The text was updated successfully, but these errors were encountered: