-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: remove dot imports from the language #29326
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
Comments
While experimenting with #20104 just a few days ago, dot imports were incredibly useful—instead of having to plumb a bunch of context through complicated code generation scripts and carefully conditionally adjust a dozen format strings, I could just dot import ssa in the relocated files. Long term, I would choose to do the work to eliminate the dot import. But it was very helpful as scaffolding. And not just for experimentation: the dot import is a single line change for reviewers, whereas qualifying every package use would have made the diff unreadable (and unrecognizable to git as a file move). To be clear (unlike type aliases), this kind of refactoring is strictly speaking possible without dot imports. It is just a lot more graceful with them. |
Yes, this is what Goa does; which is used by a lot of people. If we remove dot imports, the entire package will break. I have no issues either way. Just wanted to point this out. |
DSLs are an important use case for dot imports, I disagree that DSLs make programs harder to read. On the contrary, they make programs easier to read for specific types of program, such as the Goa examples shows, and more importantly also make a program easier to understand for non-programmer domain experts for whom the DSL has been designed. This is why I respectfully ask that this proposal be rejected. |
Although dot imports are a feature which should be used sparingly, I can't see the sense in banning them altogether. There are definitely occasions when they are useful for testing and experimentation and there are some other reasonable uses as well - see for example golang/lint#179. I often use them myself when importing the |
As per mentioned in #29036 (comment)
And to reiterate, I wouldn't mind dot-import being removed. It's just what I use them for. |
Most of the popular programming language use some form of "dot import" without any hassles. Hows ftm.Printf is more clear then simple print or echo? |
Tests are a great argument for removing dot-imports. I've seen lots of tests that failed to catch extremely redundant identifiers, because the tests and examples didn't include the package name as everyone else would. |
At first glance |
Hmm now that I think of it, I feel like I've heard a lot of this stuff before. I feel like this might be a duplicate, but I searched for proposals of removing dot imports but didn't find anything |
Dot imports make your code lie. That's Bad. When you write Also, if you then copy that code to a different file in the same package... it will fail to compile, and there's nothing goimports can do to figure out where AssertEqual comes from. So you have to Just Know™ where it comes from and add the import yourself. That's bad, too. I don't believe DSLs of the type made popular in dynamic languages such as python and ruby belong in Go. Those languages specifically give you tools so that you can make non-native functionality look like native functionality. Those languages intentionally let you play fast and loose with types and what the code actually does when you type DSLs sacrifice clarity of what the machine is doing for clarity of what the code says. That's not Go's way. You should not need external context to understand where an identifier comes from and what it is calling or doing. |
Maybe the proposal should be "flag dot imports with go vet"? @ianlancetaylor and others have asserted that Go2 shall not break existing code except where unavoidable for essential new features. |
This seems to be his proposal, haha. I think that if there's a consensus that a feature is actively harmful that it should be (at least) considered for removal. IIRC Russ Cox (maybe it was Rob Pike) have talked about removing shadowing if we get the |
Assignment redeclaration would also be flagged by Go2 vet :-) |
For now, yes, but what I was trying to say is that a full removal of a feature is something we have already considered (and seem to still be considering, looking at this proposal). Saying that significant members "have asserted that Go2 shall not break existing code [...]" makes it sound like feature removal is something that shouldn't even be listened to, rather than something we'd rather not do. |
@deanveloper I should clarify that although I opened the issue, it's really @rogpeppe 's proposal. I split out of #29036 to clarify that issue. Personally I'm somewhat against this proposal because I don't think the benefit of clearer code is worth the cost of breaking existing packages. But I'm willing to be convinced otherwise. |
I literally just learned about dot imports last week, and have been looking forward to using them in the future, mostly for the handful of random helper functions which can typically be isolated to one package. That way I can write more compact code that uses those helper functions and the code would be easier to reason about for me or anyone who pays attention to the imports. Idea: If you want to do away with dot imports, consider allowing only one dot import per file? |
For the record, the reason I bundled this issue up with #29036 was that it's necessary to do this if we want to make all imported symbols predictable, which is, I believe a useful property to have globally. I sympathise with the use of dot imports in @josharian's scenario, but I see it as somewhat of a niche case, which surely could be addressed with a little more additional tooling to make it easier to package-qualify chosen identifiers in generated code. Getting git to recognize diffs of files that move between packages is often a problem anyway. There are many such cases where code is refactored and the diffs are large because the package qualifiers change. I see that as the more general problem here, and dot-imports aren't a good solution in most such cases. In short, I think that the readability advantages of having properly qualified identifiers everywhere outweigh the occasional extra burden of adding the qualifiers. |
In my case during image manipulations I'm often using "." import as possibility to load JPEG (image/jpeg) or PNG (image/png) lib. How do you want to solve such issue? |
I don't understand your issue, can you provide an example? How are you using dot imports and why can't you just use |
@mateuszmmeteo why do you need dot import for that? Usually you write |
I am currently using lxn/walk which has a declarative package for defining forms. On the other hand, every programmer should also have the common sense to NOT have multiple dot-imports in the same GO-source. Because as long as there is only one dot-import it is PERFECTLY clear as to where the methods and types belong. I would even go further to request that the intellisense of "Visual Studio Code" be improved to understand this. |
Just FYI, GoLand already supports auto-complete on dot imports.
I agree with this in general with Go. There are a lot of cases when I really wish I could forgo having to prefix with a package name. I know this is a tangent but it would be great if we could define an alias like so:
Another thing that would be great is if Go would assume the same package when other members of the package are passed in to a func call with a simple dot prefix, e.g. instead of this:
It would be great if Go assume the follow was equal to the previous:
I'll be happy to break these two requests out into new tickets assuming I get positive reactions from those on the Go team. |
works, but will give you an exported symbol, you can always do
if you don't want that |
I am not happy that this issue still receives new debate. |
This can be viewed as feature removal. If your And |
Maybe it’s time to fork Golang project…?
|
If this issue is accepted, I will name all my dsl packages ᣟ, to be able to get a short, almost invisible import name again. With my excuses to the Canadian Aboriginals. It goes like this: package ᣟ
import "fmt"
func Yes() {
fmt.Printf("yes\n")
} ᣟ_test.go: package ᣟ_test
import "applied-maths.com/bnxt/platform/ᣟ"
import "testing"
func TestYes(t *testing.T) {
ᣟ.Yes()
} |
@StephanVerbeeck Please be polite and respectful and follow the Go Community Code of Conduct. Thanks. |
@ianlancetaylor I think we should advance this issue through the proposal process more rapidly. It's been open for over 2 years now without making much progress and this for a feature of Go that is already widely used. However, as we can see from the emoticon poll and the various discussions, there is no consensus over this issue neither in favor or against it. Perhaps it's time to apply the rule from https://github.com/golang/proposal "If general consensus cannot be reached, the proposal review group decides the next step by reviewing and discussing the issue and reaching a consensus among themselves."? Or should this proposal be placed on "hold"? |
@beoran This is an incompatible change, so it follows a different process, one that admittedly tends to move more slowly. This is indicated by the Go2 label. |
@ianlancetaylor I see, I didn't know that. how is the Go2 process different? I looked around and I could find #33892 as the umbrella issue that is tracking this, but not a formal description of the go2 process. Is that different process already formalized and written down anywhere, and if so could you refer us to it, so we can have an idea of how this issue will progress? |
It's a much more casual process, since the bar for incompatible changes is much higher. We haven't tried to formalize it. |
I guess the problem most programmers have with dot-import is not the principle. |
I use intellisense and goto-definition and they are very convenient, however as a matter of principle I don't think advanced features of any IDE should be relied on to solve code readability issues caused by dot imports. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I find dot imports extremely useful in internal packages. Given a package A:
The dependency graph looks like this (LHS is the (sub-)package, RHS are the dependencies)
For example, see the following package with such structure ( While importing |
Instead of removing 'dot import', consider adding compiler option to incorporate custom coding style spec. Each project will compile with those spec which allow or restrict 'dot import' or anything else. |
Go is a beautiful language, because it gives developers a lot of freedom, while having strict guidelines and principles. Its ecosystem is also what makes go so special. Very neat internal utilities can be created using dot imports, allowing skilled developers to write small modules that can almost act as extensions of the Go builtins; this allows very succinct code to be written. I don't see why the support for these would need to be removed in future Go versions. Is there a concrete technical or functional reason for it, or is it purely based on principle ? A language, fundamentally, allows its speakers to communicate effectively and conveniently. Intervening and removing something that is convenient for some speakers is totalitarian, and should almost never be done, and even when done, it should only be with extreme care. I understand that removing dot imports would make some Go project contributors happy, but, just like in general you wouldn't ban a syntax or word entirely from the English language (or any language whatsoever), you shouldn't ban some syntax from a programming language either. I sincerely hope that this proposal will get rejected, as it doesn't add anything to anyone, except appease some purists who, frankly, just need to look at the imports a bit, and hover their mouse over function names in their IDE. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
One use-case I thought about and that I almost went and tried today is allowing for a package to expose an API with different implementations via build constraints. So depending on the build tag, a package implementation would differ, via the dot import. (the package in question was syscall/js, basically needed an alternate implementation when building for SSR) Not unlike aliasing but coarser. Is this a relevant use-case? Another case of adding a layer of indirection to solve a problem? ;o) |
I am ok with not having those in Go v2, but let me share my usage about dot import currently. I have GraphQL on front side and SQL on DB side. type Role string
const (
RoleAdmin Role = "admin"
RoleEditor Role = "editor"
RoleUser Role = "user"
RoleGuest Role = "guest"
) With dot import it is really convenient you can just use RoleAdmin or RoleGuest on both layer, |
It's clear that there are people using dot imports today, including the standard library. Banning dot imports today would break those users. The advantage of banning dot imports is cleaner code. But there are many ways to write code that is hard to understand, and dot imports are just one way. So this proposal isn't worth doing, at least not by itself. Closing. |
This issue is broken out of #29036.
We should remove dot imports from the language (
import . "path"
). Quoting @rogpeppe:The text was updated successfully, but these errors were encountered: