-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/gofmt: consider sorting imports in the same way as goimports (separating std lib from third party) #37719
Comments
Can you give a concrete example of code formatting generated by goimports and then modified by gofmt? Thanks. |
Original package main
import (
"strings"
"github.com/gorilla/mux"
"fmt"
)
func main() {
var _ = mux.NewRouter()
fmt.Println(strings.ToUpper("Hello, world!"))
} go fmt (note the alphabetized imports) package main
import (
"fmt"
"github.com/gorilla/mux"
"strings"
)
func main() {
var _ = mux.NewRouter()
fmt.Println(strings.ToUpper("Hello, world!"))
} goimports (note the imports grouped stdlib, newline, external) package main
import (
"fmt"
"strings"
"github.com/gorilla/mux"
)
func main() {
var _ = mux.NewRouter()
fmt.Println(strings.ToUpper("Hello, world!"))
} |
I see. In that case, I believe that this may be a duplicate of #9922. |
@rsc was pretty clear in that post that I created this issue because there are currently two tools maintained in the Golang org, one pinned with Go releases and another under the x banner. I respect that not everyone may want their code formatted the way goimports does, however I think maintaining two different tools is wasted effort and confusing to newcomers and veterans alike, especially considering how popular goimports is. Hence, why I’m proposing merging the two with the implementation being an optional flag |
cc @bcmills @heschik |
My immediate reaction is that |
Perhaps I'm missing something, but I'm generally in favor of making the two tools easier to use together, but deprecating |
Interesting, I didn’t realize it removed/added imports until just now. My intent with creating this issue was solely to rectify the formatting discrepancy. Perhaps this issue could be more focused on gofmt then. |
Would it be feasible to refactor That would at least make it easier to keep the two in sync. |
That's a good point. There's even a comment which says: |
Whatever Vscode default |
@hlbbt You can configure goland to use |
I don't like goland, I prefer to use vscode, I want to add options in vscode, not goland |
I wrote one, here is the git address
|
Try this |
What version of Go are you using (
go version
)?1.14
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?MacOS
What did you do?
ran
go fmt ./..
on my codebaseWhat did you expect to see?
The builtin
go fmt
formats imports identically togoimports
What did you see instead?
go fmt
works differently thangoimports
Downloading of
goimports
is necessary to work in certain repos and adhere to their style guide. One impetus of havinggo fmt
built in is to not have these sorts of disagreements, and instead just rally around a single tool. The existence and continued support forgoimports
goes against this philosophy. In addition, there is poor visibility in the public eye ofgoimports
. Its not discoverable via the command line, as it isn't built in likevet
andfmt
. The only way people learn of this other tool is via blog posts or poking around Github.I propose that the builtin
go fmt
sorts imports in the same way asgoimports
starting with next Go versioned release, and development ofgoimports
ceases.If we want to maintain two different sets of rules, I think a good compromise would be to add a
-imports
flag to the builtingo fmt
so that we aren't maintaining two tools for one jobThe text was updated successfully, but these errors were encountered: