Skip to content
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

Alias imported package to match the package name #305

Closed
artuross opened this issue Jun 12, 2024 · 1 comment
Closed

Alias imported package to match the package name #305

artuross opened this issue Jun 12, 2024 · 1 comment

Comments

@artuross
Copy link

When the import path doesn't match the package name, it's a good idea to alias the import to be more explicit.

Here's an example with github.com/testcontainers/testcontainers-go (which defines a package testcontainers):

import "github.com/testcontainers/testcontainers-go"

should be aliased to

import testcontainers "github.com/testcontainers/testcontainers-go"

Both imports are equivalent, because in both files, package can be accessed as testcontainers.

On the other hand, if the package is already aliased like in the following example, gofumpt should not change the alias:

import tc "github.com/testcontainers/testcontainers-go"
@mvdan
Copy link
Owner

mvdan commented Mar 8, 2025

I agree with your sentiment here - and I would imagine that gopls might already do this as part of its "organize imports" action, or it might already have a feature request for it - gofumpt works only on the syntax files so it doesn't process imported packages in any way, which would be required for this to work. That would make the tool significantly more complex and slower, and it's not really about formatting, so I think it's out of scope.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants