-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
proposal: spec: allow import package name to be put after import path #68151
Comments
Why not just use a comment? |
The spec proposal allows |
Comment definitely works. But I think a package's own import path is already a good information about itself. Also it is a bit about readability and aesthetics. I will use below image to better illustrate my point: The blue bars are those well-organised import paths and red bars are various-lengthed import names. Putting import names after import path just make the code looks better imho. On the other hands, I think a package name is just a implementation detail which is in general to be put after declaration/definition. Many other languages also put alias after the importing module name/path. For example: Kotlin: import org.test.Message as TestMessage Python: import package_name.subpackage_name.module_name as pkg_mod_abbrev |
We already have a way to do import aliases. We are not going to introduce another way to do the same thing. The benefit is not worth the cost of a language change. |
If just need improves the readability. add futrue like comment alignment in gofmt that import path alignment api "k8s.io/kubernetes/pkg/apis/core"
flowcontrolv1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1"
flowcontrolv1beta1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta1"
flowcontrolv1beta2 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta2"
flowcontrolv1beta3 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta3"
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver"
hint /*options */ "k8s.io/kubernetes/pkg/controlplane/apiserver/options" |
Given that gofmt already formats struct in a similar way, it looks good enough for me. Just one thing though, how should we format when both with- and without- alias imports are mixed together? Should it be import (
"k8s.io/klog/v2"
api "k8s.io/kubernetes/pkg/apis/core"
flowcontrolv1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1"
flowcontrolv1beta1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta1"
flowcontrolv1beta2 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta2"
flowcontrolv1beta3 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta3"
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver"
"k8s.io/kubernetes/pkg/controlplane/apiserver/options"
"k8s.io/kubernetes/pkg/controlplane/controller/defaultservicecidr"
"k8s.io/kubernetes/pkg/controlplane/controller/kubernetesservice"
"k8s.io/kubernetes/pkg/controlplane/reconcilers"
"k8s.io/kubernetes/pkg/features"
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
) or import (
"k8s.io/klog/v2"
api "k8s.io/kubernetes/pkg/apis/core"
flowcontrolv1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1"
flowcontrolv1beta1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta1"
flowcontrolv1beta2 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta2"
flowcontrolv1beta3 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta3"
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver"
"k8s.io/kubernetes/pkg/controlplane/apiserver/options"
"k8s.io/kubernetes/pkg/controlplane/controller/defaultservicecidr"
"k8s.io/kubernetes/pkg/controlplane/controller/kubernetesservice"
"k8s.io/kubernetes/pkg/controlplane/reconcilers"
"k8s.io/kubernetes/pkg/features"
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
) ? @ianlancetaylor What do you think? Will PR like this be accepted? |
That would have to go through a different proposal. I don't think it's likely to be accepted. Changing gofmt is painful because it causes presubmits to break across all people using Go, especially if they are mixing versions. |
We aren't going to introduce a new syntax that exactly replicates the existing syntax in a different way. The emoji voting is not in favor. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
I like this, though. Sorting import lists in this format becomes trivial. More importantly, the reader does not have to zigzag his eyes between the two ends of each line of the import list to see what was aliased and to what, since all of the import aliases sit snugly next to all the original and unaliased package names at the rightmost ends of the lines. |
Unfortunately, this is not a change that we can make. No change in consensus, so declined. |
Go Programming Experience
Intermediate
Other Languages Experience
No response
Related Idea
Has this idea, or one like it, been proposed before?
No
Does this affect error handling?
No
Is this about generics?
No
Proposal
Allow import package name to be put after import path, i.e allow the following:
Language Spec Changes
Informal Change
No response
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
Consider the following change for example:
where all the package name aliases are put after the import path. In this way, I can tell more easily at the glance about what kinds of domains/functionalities are imported. In my opinion, this improves the readability.
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: