Skip to content

cmd/gofmt: Unexpected formatting multiline functions in struct literal #23523

@andrewhare

Description

@andrewhare

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

go version go1.9.2 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/drew/go"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g3/0kqyss7j5zj0dj3gdkz2m9lw0000gn/T/go-build269266413=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Run gofmt on the following code:

package main

type x struct {
	i        int
	someFunc func()
}

func main() {
	_ = &x{
		i: 1,
		someFunc: func() {
			_ = 1
			_ = 2
		},
	}
	_ = &x{
		i:        1,
		someFunc: func() {},
	}
}

What did you expect to see?

package main

type x struct {
	i        int
	someFunc func()
}

func main() {
	_ = &x{
		i: 	  1,
		someFunc: func() {
			_ = 1
			_ = 2
		},
	}
	_ = &x{
		i:        1,
		someFunc: func() {},
	}
}

Notice the first struct literal, that the value for i is indented to match someFunc as it is in the second literal.

What did you see instead?

The file remains unchanged. This seems to only happen when a multiline function is declared inline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions