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

errors: As should allow target to be a nil pointer to a concrete type #31541

bcmills opened this issue Apr 18, 2019 · 1 comment


None yet
1 participant
Copy link

commented Apr 18, 2019

What did you do?$ cat >./main.go <<EOF
package main

import (

func main() {
        const addr = "••••••••"
        conn, err := net.Dial("tcp", addr)
        if err != nil {
                if errors.As(err, (*net.InvalidAddrError)(nil)) {
                        fmt.Println("invalid address: %s")
        defer conn.Close()
EOF$ cat >./go.mod <<EOF

go 1.12
EOF$ go version
go version devel +33fdc10a30 Wed Apr 17 13:37:58 2019 -0400 linux/amd64$ go run main.go

What did you expect to see?

errors.As being useful for type-checks even if I don't want to allocate space to store the result.

For this specific example:

invalid address: ••••••••

What did you see instead?

panic: errors: target must be a non-nil pointer

goroutine 1 [running]:
errors.As(0x54de60, 0xc000098000, 0x507020, 0x0, 0x0)
        /usr/local/google/home/bcmills/go/src/errors/wrap.go:86 +0x5eb
        /tmp/tmp.8hp5hhWBDQ/ +0xd5
exit status 2

This is the documented behavior of errors.As, but it seems strictly less useful than allowing a nil pointer of a concrete type.

@bcmills bcmills added this to the Go1.13 milestone Apr 18, 2019


This comment has been minimized.

Copy link
Member Author

commented Apr 18, 2019

(See previously #30970, which relaxed a somewhat related constraint on err.)

CC @neild @jba @mpvl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.