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

proposal: group standard library imports together in gofmt #27365

Closed
ghost opened this issue Aug 30, 2018 · 6 comments
Closed

proposal: group standard library imports together in gofmt #27365

ghost opened this issue Aug 30, 2018 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 30, 2018

What version of Go are you using (go version)?

go version go1.11 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\htr\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\htr\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\htr\AppData\Local\Temp\go-build186502278=/tmp/go-build -gno-record-gcc-switches

What did you do?

$ gofmt foo.go

foo.go:

package main

import (
	"github.com/google/uuid"

	"net/http"

	"fmt"
)

func main() {
	fmt.Printf("%v %v\n", http.StatusOK, uuid.New().String())
}

https://play.golang.org/p/GwCJH53FWxV

What did you expect to see?

The import statements should be ordered like:

import (
	"fmt"
	"net/http"

	"github.com/google/uuid"
)

What did you see instead?

Actual result:

import (
	"github.com/google/uuid"

	"net/http"

	"fmt"
)
@ghost
Copy link
Author

ghost commented Aug 30, 2018

If there are no empty lines in the import statements gofmt works as expected:

package main

import (
	"github.com/google/uuid"
	"net/http"
	"fmt"
)

func main() {
	fmt.Printf("%v %v\n", http.StatusOK, uuid.New().String())
}

https://play.golang.org/p/v6cZIntMhgY

@go101
Copy link

go101 commented Aug 30, 2018

Is this recent change in "go fmt"? I remember that the result is like what OP expects before.

[edit]: Aha, didn't notice "go fmt" support grouping before. Doesn't OP's request has less freedom?

@mvdan
Copy link
Member

mvdan commented Aug 30, 2018

Similar ideas have been rejected in the past - see #26846.

Have you considered proposing this for goimports? It already has functionality to split import groups - see its -local flag.

@FiloSottile FiloSottile changed the title Go fmt doesn't organize import statements as expected proposal: group standard library imports together in gofmt Aug 30, 2018
@gopherbot gopherbot added this to the Proposal milestone Aug 30, 2018
@ghost
Copy link
Author

ghost commented Aug 31, 2018

The same issue is present in goimport. If the problem can be fixed there as well that would be great.

@agnivade
Copy link
Contributor

Russ has rejected this for goimports too in #26846.

We intentionally leave this to local custom - gofmt and goimports will never move imports across blank lines in the import block. So if I want path with the others, I can do that, and gofmt/goimports respect that. And if you want all your renamed imports together, you can do that, and gofmt/goimports respect that too.

I doubt anything has changed since then.

@rsc
Copy link
Contributor

rsc commented Sep 19, 2018

As @agnivade points out, no, sorry, for same reasons as #26846.

@rsc rsc closed this as completed Sep 19, 2018
@golang golang locked and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants