Go version
1.24.4
Output of go env
in your module/workspace:
AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/felipe/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/felipe/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/fr/61jb9pj52bs_r6q_5dlc4bxw0000gp/T/go-build3339825284=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/felipe/go/src/github.com/10gen/custom-mongomirror/go.mod'
GOMODCACHE='/Users/felipe/go/pkg/mod'
GONOPROXY='github.com/10gen/'
GONOSUMDB='github.com/10gen/'
GOOS='darwin'
GOPATH='/Users/felipe/go'
GOPRIVATE='github.com/10gen/'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/felipe/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
Try to compile this:
package main
import (
"errors"
"fmt"
)
type MyError struct{}
func (m MyError) Error() string {
return ""
}
var _ error = MyError{}
func makeError() error {
return nil
}
func main() {
err := fmt.Errorf("")
if my := MyError{}; errors.As(err, &my) {
fmt.Printf("match")
}
}
What did you see happen?
error: expected boolean expression, found assignment (missing parentheses around composite literal?)
What did you expect to see?
It should compile & run.
It seems the braces might be confusing something. If I replace MyError{}
with *new(MyError)
then it works.
Comment From: FGasper
For posterity: it seems this is just a limitation of Go’s parser. (It’d be sweet, though, if a more helpful error message could be given.)
Comment From: ianlancetaylor
Search for "parsing ambiguity" in https://go.dev/ref/spec#Composite_literals.
The error message tells you exactly how to fix the problem. Do you have any suggestions for how to improve it? Thanks.