Skip to content

cmd/gofmt: Slice access not being formatted to reflect operator precedence #60594

Open
@richgrov

Description

@richgrov

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

$ go version
go version go1.20.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""                                                                                                                                                                                                                                           
GOARCH="amd64"                                                                                                                                                                                                                                                
GOBIN=""
GOCACHE="~/.cache/go-build"
GOENV="~/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="~/Desktop/oneworld/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build41908708=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/7EnkFFdEfKt

gofmt (or clicking the "Format" button on the link) will insert spaces between operators to indicate their precedence (line 7). However, this does not apply to a slice accessor (line 10).

This can make code hard to read in more complex operations, such as converting cartesian coordinates to an index (example below.)

What did you expect to see?

func (arr *Array3d) getByte(x, y, z int) byte {
  return arr.data[x*arr.width*arr.height + z*arr.height + y)
}

What did you see instead?

func (arr *Array3d) getByte(x, y, z int) byte {
  return arr.data[x*arr.width*arr.height+z*arr.height+y]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions