You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proposal: Go 2: remove dot imports from the language #29326
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.Proposalv2An incompatible library change
We should remove dot imports from the language (import . "path"). Quoting @rogpeppe:
The official guidelines suggest that a dot import should only be used "to let the file pretend to be part of package foo even though it is not". This is a stylistic choice and strictly unnecessary. The tests can still use package-qualified names with only minor inconvenience (the same inconvenience that any external user will see).
Other than that, I believe the most common use is to make the imported package feel "close to a DSL". This seems to be actively opposed to the aims of Go, as it makes the programs much harder to read.
Reacted by Brad Fitzpatrick, Dave Cheney, Bryan C. Mills, jimmyfrasche, Joe Tsai, Daniel Martí, reusee, bronze1man, Matt Layher, cenxui lin and 85 more
Reacted by Josh Bleecher Snyder, Beoran, Viktor Kojouharov, Peter, alanfo, Liam Breck, Ahmed W., Daniel Theophanes, 方泓睿, Cody Oss and 84 more
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.
Reacted by alanfo, forseti, Bryan C. Mills, Liam Breck, Ivan Borshukov, Daniel Theophanes, Mike Schinkel, Amine Benmouffok, Nathan Marley, Owais Lone and 15 more
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.
Reacted by alanfo, forseti, Mike Schinkel, Andrés Rodríguez, Owais Lone, Meghan Denny, TheDiveO, Felipe Castro, Miroslav Kravec, Kaveh Shahbazian and 8 more
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 math package, so I can just write Cos instead of math.Cos. Admittedly this isn't the greatest of use cases but, for me at least, there is no risk of confusion as I would never use the names of math functions as public names in my own packages.
Reacted by Liam Breck, Ben, Mike Schinkel, TheDiveO, Qiang Li, deef, Kaveh Shahbazian, effinsky, Danilo, Seyit Gokce and 1 more
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?
Reacted by JiangQH and effinsky
Reacted by Tv, Lanre Adelowo, Peter, Meghan Denny, Anton Zaviyalov, Mauro Toffanin, Miroslav Kravec, Edvard LUNDIN, Mae Kennedy, Jon Williams and 2 more
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.
Reacted by Tv, Caleb Spare, Nic Cope, fang duan, Mauro Toffanin, Miroslav Kravec, Diego Novaes, Ilia Choly and Fumin
At first glance math seems like a great argument for not removing them, but then consider functions like Jn, Y0, and Log: the former two are somewhat obscure, and the latter could easily collide with a similarly-named function in another package. I could see a reasonable argument for dot-importing trigonometric functions and rounding functions, but math overall seems like a bit of a stretch.
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
When you write AssertEqual(t, got, expected) and your current package doesn't have an AssertEqual function... that code is lying. If you're reading that in a code review (i.e. not in an IDE with hover text etc), you would not be crazy to think AssertEqual is some function in the current package.
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 foo += bar can be vastly different depending on what foo and bar are. Go is not that language. foo += bar will always be simple to understand in Go. There's only a couple things it can possibly mean.
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.
Reacted by Tom Thorogood, bronze1man, Gregory Man, Rodolfo Carvalho, Andy Bursavich, Nic Cope, Brent , Jacob, Derek Lemon, Mauro Toffanin and 9 more
Reacted by Mike Schinkel, Beoran, Zubin Madon, Meghan Denny, Peter Kappler, deef, Kaveh Shahbazian, Matthew Olenik, Seyit Gokce, codemagicians and 1 more
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.
Reacted by Mike Schinkel, George Moschovitis, fang duan, Frédéric G. MARAND, Derek Lemon, Mauro Toffanin, deef, Felipe Castro, Miroslav Kravec, Martin Tournoij and 2 more
I guess the problem most programmers have with dot-import is not the principle.
Rather I guess they have issues with the fact that code intellisense does never implement it (not working in visual studio code) so Go-users consider the dot-import to be broken while it is rather the code intellisense that is broken.
It is indeed a nuisance that goto-definition is not working for these where this is clearly a decision of the implementor to ignore this because he/she himself/herself is not using it.
So maybe the solution to this topic is to finish the last 10% work rather than undoing the working 90%.
Rather I guess they have issues with the fact that code intellisense does never implement it (not working in visual studio code) so Go-users consider the dot-import to be broken while it is rather the code intellisense that is broken.
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.
While importing A/internal as internal makes sense in implementation code, importing A/internal/test and A/internal/test/shared named makes less sense and probably requires aliases to avoid clashes with e.g. external pkg/test. Dot imports make this way cleaner and clearer
Reacted by Beoran, Herczeg Zsolt, Laurier Lavoie-Giasson and PK
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.
Reacted by Tv, Jason Fesler, deanveloper, TheDiveO, fsouza, esdnm, Sergey Zhdanov and geraba
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.
Reacted by rabani111, David Ringuet, Denis, Gacel, kolbycrouch, Guillem, Seyit Gokce, Doug Saylor, Dmitrii Okunev, David Pesta and 1 more
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)
With dot import it is really convenient you can just use RoleAdmin or RoleGuest on both layer,
and it is much easier for code generations, which does not need to know details or receiver/pkg's type.
Also having those in separate package, you can import from anywhere without worrying about cyclic imports.
You may think that I should use Role as Int, but that is another story.
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.
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.Proposalv2An incompatible library change
Activity
josharian commentedon Dec 19, 2018
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.
agnivade commentedon Dec 19, 2018
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.
beoran commentedon Dec 19, 2018
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.
alanfo commentedon Dec 19, 2018
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
mathpackage, so I can just writeCosinstead ofmath.Cos. Admittedly this isn't the greatest of use cases but, for me at least, there is no risk of confusion as I would never use the names of math functions as public names in my own packages.deanveloper commentedon Dec 19, 2018
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.
gotzmann commentedon Dec 19, 2018
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?
bcmills commentedon Dec 19, 2018
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.
bcmills commentedon Dec 19, 2018
At first glance
mathseems like a great argument for not removing them, but then consider functions likeJn,Y0, andLog: the former two are somewhat obscure, and the latter could easily collide with a similarly-named function in another package. I could see a reasonable argument for dot-importing trigonometric functions and rounding functions, butmathoverall seems like a bit of a stretch.deanveloper commentedon Dec 19, 2018
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
natefinch commentedon Dec 19, 2018
Dot imports make your code lie. That's Bad.
When you write
AssertEqual(t, got, expected)and your current package doesn't have an AssertEqual function... that code is lying. If you're reading that in a code review (i.e. not in an IDE with hover text etc), you would not be crazy to think AssertEqual is some function in the current package.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
foo += barcan be vastly different depending on what foo and bar are. Go is not that language.foo += barwill always be simple to understand in Go. There's only a couple things it can possibly mean.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.
networkimprov commentedon Dec 19, 2018
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.
56 remaining items
StephanVerbeeck commentedon Mar 3, 2021
I guess the problem most programmers have with dot-import is not the principle.
Rather I guess they have issues with the fact that code intellisense does never implement it (not working in visual studio code) so Go-users consider the dot-import to be broken while it is rather the code intellisense that is broken.
It is indeed a nuisance that goto-definition is not working for these where this is clearly a decision of the implementor to ignore this because he/she himself/herself is not using it.
So maybe the solution to this topic is to finish the last 10% work rather than undoing the working 90%.
drekle commentedon Mar 3, 2021
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.
josharian commentedon Apr 1, 2021
josharian commentedon Apr 2, 2021
rvolosatovs commentedon Apr 15, 2021
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 (
Abeingpkg/networkserver, andA/Bbeingpkg/networkserver/redis)https://github.com/TheThingsNetwork/lorawan-stack/tree/bb0f4c7cc2c8c71efe4140ac9078cfb2ee1ddba2/pkg/networkserver
While importing
A/internalasinternalmakes sense in implementation code, importingA/internal/testandA/internal/test/sharednamed makes less sense and probably requires aliases to avoid clashes with e.g. externalpkg/test. Dot imports make this way cleaner and clearerjindanupajit commentedon Jan 3, 2022
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.
the-zucc commentedon Jul 15, 2022
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.
seyitgkc commentedon Oct 27, 2022
the-zucc commentedon Oct 27, 2022
atdiar commentedon Oct 30, 2022
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)
S-YOU commentedon Nov 18, 2022
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.
I would like share Enums which type defined like this, for example.
With dot import it is really convenient you can just use RoleAdmin or RoleGuest on both layer,
and it is much easier for code generations, which does not need to know details or receiver/pkg's type.
Also having those in separate package, you can import from anywhere without worrying about cyclic imports.
You may think that I should use Role as Int, but that is another story.
ianlancetaylor commentedon Aug 22, 2023
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.