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

Cannot use errors.As() with new #71331

Closed
andig opened this issue Jan 19, 2025 · 2 comments
Closed

Cannot use errors.As() with new #71331

andig opened this issue Jan 19, 2025 · 2 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@andig
Copy link
Contributor

andig commented Jan 19, 2025

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

$ go version
go version go1.23.5 darwin/arm64

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='arm64'
GOBIN=''
GOCACHE='/Users/andig/Library/Caches/go-build'
GOENV='/Users/andig/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/andig/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/andig/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.5/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.5/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.5'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/andig/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/andig/htdocs/evcc/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build3430922146=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOROOT/bin/go version: go version go1.23.5 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.23.5
uname -v: Darwin Kernel Version 24.2.0: Fri Dec  6 18:40:14 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8103
ProductName:		macOS
ProductVersion:		15.2
BuildVersion:		24C101
lldb --version: lldb-1600.0.39.109
Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)

What did you do?

Check for error return type using errors.As(err, new(Err)): https://play.golang.com/p/nlxAmD6LtLm

What did you expect to see?

Successful compile

What did you see instead?

second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type

It seems that the second parameter is a non-nil pointer and hence should compile or the error message be improved. I understand that I can use and error variable of the specific type but since the actual typed error is not needed it would be nice if the anonymous new could be used here.

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Jan 19, 2025
@andig andig closed this as completed Jan 19, 2025
@andig
Copy link
Contributor Author

andig commented Jan 19, 2025

The trick here is

pointer to either a type that implements error

hence it would need pointer to pointer receiver. Or rather implement error on the base receiver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

2 participants