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

cmd/gofmt : gofmt issue with new generics #51898

Closed
nagnagendra opened this issue Mar 23, 2022 · 2 comments
Closed

cmd/gofmt : gofmt issue with new generics #51898

nagnagendra opened this issue Mar 23, 2022 · 2 comments

Comments

@nagnagendra
Copy link

nagnagendra commented Mar 23, 2022

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

$ go version

go version go1.18 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/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.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1871870308=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have written some code using go generics which is recently introduced in 1.18
While formatting the code using gofmt we're getting error as below

Error : sample.go:26:21: expected '(', found '['

Sample generic function :

func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
   var s V
   for _, v := range m {
       s += v
   }
   return s
} 
@ALTree
Copy link
Member

ALTree commented Mar 23, 2022

You probably have an old gofmt binary somewhere. That code snippet is formatted correctly with Go1.18's gofmt. That error is what you got on the old one, that didn't support type parameters.

@ALTree ALTree closed this as completed Mar 23, 2022
@khinshankhan
Copy link

If it's any help, my editor was using goimports before, and now it uses gofmt with the -s flag and works perfectly.

My go version is currently go version go1.19.3 darwin/amd64.

@golang golang locked and limited conversation to collaborators Dec 3, 2023
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

4 participants